hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

stream.ha (1020B)


      1 // License: MPL-2.0
      2 // (c) 2021 Bor Grošelj Simić <bor.groseljsimic@telemach.net>
      3 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      4 use strings;
      5 
      6 const teststream_vtable: vtable = vtable {
      7 	reader = &teststream_read,
      8 	writer = &teststream_write,
      9 	...
     10 };
     11 
     12 type teststream = struct {
     13 	stream: stream,
     14 	r: size,
     15 	nreads: size,
     16 	w: size,
     17 	nwrites: size,
     18 };
     19 
     20 fn teststream_open() teststream = teststream {
     21 	stream = &teststream_vtable,
     22 	...
     23 };
     24 
     25 fn teststream_read(s: *stream, buf: []u8) (size | EOF | error) = {
     26 	let stream = s: *teststream;
     27 	stream.r += len(buf);
     28 	stream.nreads += 1;
     29 	return len(buf);
     30 };
     31 
     32 fn teststream_write(s: *stream, buf: const []u8) (size | error) = {
     33 	let stream = s: *teststream;
     34 	stream.w += len(buf);
     35 	stream.nwrites += 1;
     36 	return len(buf);
     37 };
     38 
     39 fn teststream_write_short(s: *stream, buf: const []u8) (size | error) = {
     40 	let stream = s: *teststream;
     41 	stream.nwrites += 1;
     42 	if (len(buf) > 10) {
     43 		stream.w += len(buf) / 2;
     44 		return len(buf) / 2;
     45 	} else {
     46 		stream.w += len(buf);
     47 		return len(buf);
     48 	};
     49 };