hare

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

stream_test.ha (961B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 const teststream_vtable: vtable = vtable {
      5 	reader = &teststream_read,
      6 	writer = &teststream_write,
      7 	...
      8 };
      9 
     10 type teststream = struct {
     11 	stream: stream,
     12 	r: size,
     13 	nreads: size,
     14 	w: size,
     15 	nwrites: size,
     16 };
     17 
     18 fn teststream_open() teststream = teststream {
     19 	stream = &teststream_vtable,
     20 	...
     21 };
     22 
     23 fn teststream_read(s: *stream, buf: []u8) (size | EOF | error) = {
     24 	let stream = s: *teststream;
     25 	stream.r += len(buf);
     26 	stream.nreads += 1;
     27 	return len(buf);
     28 };
     29 
     30 fn teststream_write(s: *stream, buf: const []u8) (size | error) = {
     31 	let stream = s: *teststream;
     32 	stream.w += len(buf);
     33 	stream.nwrites += 1;
     34 	return len(buf);
     35 };
     36 
     37 fn teststream_write_short(s: *stream, buf: const []u8) (size | error) = {
     38 	let stream = s: *teststream;
     39 	stream.nwrites += 1;
     40 	if (len(buf) > 10) {
     41 		stream.w += len(buf) / 2;
     42 		return len(buf) / 2;
     43 	} else {
     44 		stream.w += len(buf);
     45 		return len(buf);
     46 	};
     47 };