tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

114 lignes · 3271 octetsBrut · Historique
1/// Console input and output.
2///
3/// Output never blocks a scheduler thread (ADR 0022), and neither does
4/// reading standard input (ADR 0086): `read_line` and `read_bytes` park
5/// the calling actor until a reader thread has read something. Input is
6/// read ahead in a bounded buffer, so a program that stops reading stops
7/// the reading. Actors that read at once each get distinct lines; an actor
8/// killed while it waits takes nothing.
9
10@extern c "tl_println"
11pub fn println :: text: String -> () / <io>
12
13@extern c "tl_print"
14pub fn print :: text: String -> () / <io>
15
16@extern c "tl_eprintln"
17pub fn eprintln :: text: String -> () / <io>
18
19import std/bytes
20import std/ffi.{type Resource}
21
22/// Why standard input could not be read.
23@derive Eq Show Debug
24pub type Error
25{ /// A line longer than `max` bytes: it was skipped, up to its newline.
26 TooLong(max: Int)
27 /// A line that is not UTF-8 (`bytes.to_string`'s message); it was
28 /// skipped. `read_bytes` reads any bytes.
29, NotUtf8(String)
30 /// The system refused to read (its message). Input ends after it.
31, Failed(String)
32}
33
34/// The longest line `read_line` reads: 16 MiB.
35pub fn max_line_length :: -> Int
36{
37 16777216
38}
39
40/// The next line of standard input, without its `\n` (or `\r\n`);
41/// `None` at the end of input. A last line without `\n` is still a line.
42pub fn read_line :: -> Result<Option<String>, Error> / <io>
43{
44 read_line_max (max_line_length ())
45}
46
47/// `read_line`, where a line longer than `max` bytes (without its
48/// newline; at most `max_line_length ()`) is `TooLong`.
49pub fn read_line_max :: max: Int -> Result<Option<String>, Error> / <io>
50{
51 let max = line_limit max
52 let (kind, data) = read_line_raw max
53 match kind
54 { 0 -> match bytes.to_string data
55 { Ok(line) -> Ok(Some(line)), Error(e) -> Error(NotUtf8(e)) }
56 , 1 -> Ok(None)
57 , 2 -> Error(TooLong(max))
58 , _ -> Error(Failed(bytes.to_string_lossy data))
59 }
60}
61
62/// Up to `max` bytes of standard input (at least one), as soon as there
63/// are any; `None` at the end of input.
64pub fn read_bytes :: max: Int -> Result<Option<Bytes>, Error> / <io>
65{
66 let (kind, data) = read_bytes_raw max
67 match kind
68 { 0 -> Ok(Some(data))
69 , 1 -> Ok(None)
70 , _ -> Error(Failed(bytes.to_string_lossy data))
71 }
72}
73
74/// `max` within 0 and `max_line_length ()`.
75fn line_limit :: max: Int -> Int
76{
77 if max < 0
78 {
79 0
80 }
81 else
82 {
83 if max > max_line_length ()
84 {
85 max_line_length ()
86 }
87 else
88 {
89 max
90 }
91 }
92}
93
94// Each returns (kind, data): 0 data, 1 the end of input, 2 a line too
95// long, 3 a read error (its message).
96@extern c "tl_io_read_line"
97fn read_line_raw :: max: Int -> (Int, Bytes) / <io>
98
99@extern c "tl_io_read_bytes"
100fn read_bytes_raw :: max: Int -> (Int, Bytes) / <io>
101
102/// A byte stream: a TCP connection (`std/net`) or a TLS one (`std/tls`),
103/// keyed by its marker type, so code can take either:
104///
105/// fn echo(conn: Resource<s>) -> () / <io> where Stream<s> { ... }
106pub trait Stream<s>
107{
108 /// Up to `max` bytes; empty at the end of the stream.
109 fn read :: conn: Resource<s> max: Int -> Result<Bytes, String> / <io>
110 /// All of `data`.
111 fn write :: conn: Resource<s> data: Bytes -> Result<(), String> / <io>
112 /// Close the stream.
113 fn close :: conn: Resource<s> -> () / <io>
114}