Debian Codegraph @

io/+netbsd/vector.ha

raw
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 // Returns the byte slice a [[vector]] refers to.
17 export fn vector_buf(vec: *vector) []u8 = {
18 return (vec.iov_base: *[*]u8)[..vec.iov_len];
19 };
20
21 // Performs a vectored read on the given file. A read is performed on each of
22 // the vectors, prepared with [[mkvector]], in order, and the total number of
23 // bytes read is returned.
24 export fn readv(fd: file, vectors: vector...) (size | EOF | error) = {
25 if (len(vectors) > types::INT_MAX: size) {
26 return errors::invalid;
27 };
28 match (rt::readv(fd, vectors: *[*]rt::iovec, len(vectors): int)) {
29 case let err: rt::errno =>
30 return errors::errno(err);
31 case let n: size =>
32 switch (n) {
33 case 0 =>
34 return EOF;
35 case =>
36 return n;
37 };
38 };
39 };
40
41 // Performs a vectored write on the given file. Each of the vectors, prepared
42 // with [[mkvector]], are written to the file in order, and the total number of
43 // bytes written is returned.
44 export fn writev(fd: file, vectors: vector...) (size | error) = {
45 if (len(vectors) > types::INT_MAX: size) {
46 return errors::invalid;
47 };
48 match (rt::writev(fd, vectors: *[*]rt::iovec, len(vectors): int)) {
49 case let err: rt::errno =>
50 return errors::errno(err);
51 case let n: size =>
52 return n;
53 };
54 };