hare

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

vector.ha (1348B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use rt;
      6 use types;
      7 
      8 export type vector = rt::iovec;
      9 
     10 // Creates a vector for use with [[writev]] and [[readv]].
     11 export fn mkvector(buf: []u8) vector = vector {
     12 	iov_base = buf: *[*]u8,
     13 	iov_len = len(buf),
     14 };
     15 
     16 // Performs a vectored read on the given file. A read is performed on each of
     17 // the vectors, prepared with [[mkvector]], in order, and the total number of
     18 // bytes read is returned.
     19 export fn readv(fd: file, vectors: vector...) (size | EOF | error) = {
     20 	if (len(vectors) > types::INT_MAX: size) {
     21 		return errors::invalid;
     22 	};
     23 	match (rt::readv(fd, vectors: *[*]rt::iovec, len(vectors): int)) {
     24 	case let err: rt::errno =>
     25 		return errors::errno(err);
     26 	case let n: size =>
     27 		switch (n) {
     28 		case 0 =>
     29 			return EOF;
     30 		case =>
     31 			return n;
     32 		};
     33 	};
     34 };
     35 
     36 // Performs a vectored write on the given file. Each of the vectors, prepared
     37 // with [[mkvector]], are written to the file in order, and the total number of
     38 // bytes written is returned.
     39 export fn writev(fd: file, vectors: vector...) (size | error) = {
     40 	if (len(vectors) > types::INT_MAX: size) {
     41 		return errors::invalid;
     42 	};
     43 	match (rt::writev(fd, vectors: *[*]rt::iovec, len(vectors): int)) {
     44 	case let err: rt::errno =>
     45 		return errors::errno(err);
     46 	case let n: size =>
     47 		return n;
     48 	};
     49 };