hare

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

commit fa6bf9f8ce373f4b1b19d340e086d04168ee3c0d
parent 128e8506a9cd343e0b576fa4a7aa5602a3014a97
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat,  4 Sep 2021 09:34:44 +0200

os: embed io::stream in fdstream

Also renames it to remove the _, which is more consistent with other
streams.

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mos/+linux/fdstream.ha | 49+++++++++++++++++++++++++------------------------
Mos/+linux/stdfd.ha | 6+++---
2 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/os/+linux/fdstream.ha b/os/+linux/fdstream.ha @@ -3,39 +3,40 @@ use io; use rt; use strings; -type fd_stream = struct { - stream: io::stream, +export type fdstream = struct { + io::stream, fd: int, }; // Opens a Unix file descriptor as an io::stream. export fn fdopen(fd: int, name: str, mode: io::mode) *io::stream = { - let stream = alloc(fd_stream { ... }); + let stream = alloc(fdstream { ... }); static_fdopen(fd, strings::dup(name), mode, stream); - stream.stream.closer = &fd_close; - return &stream.stream; + stream.closer = &fd_close; + return stream; }; fn static_fdopen( - fd: int, name: str, mode: io::mode, stream: *fd_stream, + fd: int, + name: str, + mode: io::mode, + stream: *fdstream, ) *io::stream = { - *stream = fd_stream { - stream = io::stream { - name = name, - closer = &fd_close_static, - copier = &fd_copy, - seeker = &fd_seek, - ... - }, + *stream = fdstream { + name = name, + closer = &fd_close_static, + copier = &fd_copy, + seeker = &fd_seek, fd = fd, + ... }; if (mode & io::mode::READ == io::mode::READ) { - stream.stream.reader = &fd_read; + stream.reader = &fd_read; }; if (mode & io::mode::WRITE == io::mode::WRITE) { - stream.stream.writer = &fd_write; + stream.writer = &fd_write; }; - return &stream.stream; + return stream; }; fn is_fdstream(s: *io::stream) bool = { @@ -63,12 +64,12 @@ export fn streamfd(s: *io::stream, unwrap: bool) (int | void) = { if (!is_fdstream(s)) { return; }; - let stream = s: *fd_stream; + let stream = s: *fdstream; return stream.fd; }; fn fd_read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = { - let stream = s: *fd_stream; + let stream = s: *fdstream; return match (rt::read(stream.fd, buf: *[*]u8, len(buf))) { err: rt::errno => errors::errno(err), n: size => switch (n) { @@ -79,7 +80,7 @@ fn fd_read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = { }; fn fd_write(s: *io::stream, buf: const []u8) (size | io::error) = { - let stream = s: *fd_stream; + let stream = s: *fdstream; return match (rt::write(stream.fd, buf: *const [*]u8, len(buf))) { err: rt::errno => errors::errno(err), n: size => n, @@ -87,14 +88,14 @@ fn fd_write(s: *io::stream, buf: const []u8) (size | io::error) = { }; fn fd_close(s: *io::stream) void = { - let stream = s: *fd_stream; + let stream = s: *fdstream; rt::close(stream.fd)!; free(s.name); free(stream); }; fn fd_close_static(s: *io::stream) void = { - let stream = s: *fd_stream; + let stream = s: *fdstream; rt::close(stream.fd)!; free(stream); }; @@ -106,7 +107,7 @@ fn fd_copy(to: *io::stream, from: *io::stream) (size | io::error) = { return errors::unsupported; }; - let to = to: *fd_stream, from = from: *fd_stream; + let to = to: *fdstream, from = from: *fdstream; let sum = 0z; for (true) { let n = match (rt::sendfile(to.fd, from.fd, @@ -135,7 +136,7 @@ fn fd_seek( off: io::off, whence: io::whence, ) (io::off | io::error) = { - let stream = s: *fd_stream; + let stream = s: *fdstream; return match (rt::lseek(stream.fd, off: i64, whence: uint)) { err: rt::errno => errors::errno(err), n: i64 => n: io::off, diff --git a/os/+linux/stdfd.ha b/os/+linux/stdfd.ha @@ -1,9 +1,9 @@ use bufio; use io; -let static_stdin_fd: fd_stream = fd_stream { ... }; -let static_stdout_fd: fd_stream = fd_stream { ... }; -let static_stderr_fd: fd_stream = fd_stream { ... }; +let static_stdin_fd: fdstream = fdstream { ... }; +let static_stdout_fd: fdstream = fdstream { ... }; +let static_stderr_fd: fdstream = fdstream { ... }; let static_stdin_bufio: bufio::bufstream = bufio::bufstream { ... }; let static_stdout_bufio: bufio::bufstream = bufio::bufstream { ... };