hare

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

commit 2b16cd8c2e5fb34344cfc28a1a1402937f363e1b
parent a7528cceb3c95f2acbdae5da40f57430febb0c38
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 10 Apr 2021 20:06:14 -0400

bufio, fdstream, dirfdfs: reorder declarations

This puts the most important declarations upfront, which is the style
that we've been aiming for ever since implementing forward references.

Might want to mention this in the style guide.

Diffstat:
Mbufio/buffered.ha | 44++++++++++++++++++++++----------------------
Mos/+linux/dirfdfs.ha | 34+++++++++++++++++-----------------
Mos/+linux/fdstream.ha | 16++++++++--------
3 files changed, 47 insertions(+), 47 deletions(-)

diff --git a/bufio/buffered.ha b/bufio/buffered.ha @@ -15,6 +15,28 @@ export type bufstream = struct { unread: []u8, }; +// Creates a stream which buffers reads and writes for the underlying stream. +// This is generally used to improve performance of small reads/writes for +// sources where I/O operations are costly, such as if they invoke a syscall or +// take place over the network. +// +// The caller should supply one or both of a read and write buffer as a slice of +// the desired buffer, or empty slices if read or write functionality is +// disabled. The same buffer may not be used for both reads and writes. +// +// The caller is responsible for closing the underlying stream and freeing the +// provided buffers after the buffered stream is closed. +export fn buffered( + src: *io::stream, + rbuf: []u8, + wbuf: []u8, +) *io::stream = { + let s = alloc(bufstream { ... }); + let st = static_buffered(src, rbuf, wbuf, s); + st.closer = &buffered_close; + return st; +}; + export fn static_buffered( src: *io::stream, rbuf: []u8, @@ -47,28 +69,6 @@ export fn static_buffered( return &s.stream; }; -// Creates a stream which buffers reads and writes for the underlying stream. -// This is generally used to improve performance of small reads/writes for -// sources where I/O operations are costly, such as if they invoke a syscall or -// take place over the network. -// -// The caller should supply one or both of a read and write buffer as a slice of -// the desired buffer, or empty slices if read or write functionality is -// disabled. The same buffer may not be used for both reads and writes. -// -// The caller is responsible for closing the underlying stream and freeing the -// provided buffers after the buffered stream is closed. -export fn buffered( - src: *io::stream, - rbuf: []u8, - wbuf: []u8, -) *io::stream = { - let s = alloc(bufstream { ... }); - let st = static_buffered(src, rbuf, wbuf, s); - st.closer = &buffered_close; - return st; -}; - // Flushes pending writes to the underlying stream. export fn flush(s: *io::stream) (io::error | void) = { assert(s.writer == &buffered_write, diff --git a/os/+linux/dirfdfs.ha b/os/+linux/dirfdfs.ha @@ -43,6 +43,23 @@ type os_filesystem = struct { resolve: resolve_flags, }; +// Opens a file descriptor as an [fs::fs]. This file descriptor must be a +// directory file. The file will be closed when the fs is closed. +// +// If no other flags are provided to [fs::open] and [fs::create] when used with +// a dirfdfs, [fs::flags::NOCTTY] and [fs::flags::CLOEXEC] are used when opening +// the file. If you pass your own flags, it is recommended that you add these +// unless you know that you do not want them. +export fn dirfdopen(fd: int, resolve: resolve_flags...) *fs::fs = { + let ofs = alloc(os_filesystem { ... }); + let fs = static_dirfdopen(fd, ofs); + for (let i = 0z; i < len(resolve); i += 1) { + ofs.resolve |= resolve[i]; + }; + fs.close = &fs_close; + return fs; +}; + fn static_dirfdopen(fd: int, filesystem: *os_filesystem) *fs::fs = { *filesystem = os_filesystem { fs = fs::fs { @@ -64,23 +81,6 @@ fn static_dirfdopen(fd: int, filesystem: *os_filesystem) *fs::fs = { return &filesystem.fs; }; -// Opens a file descriptor as an [fs::fs]. This file descriptor must be a -// directory file. The file will be closed when the fs is closed. -// -// If no other flags are provided to [fs::open] and [fs::create] when used with -// a dirfdfs, [fs::flags::NOCTTY] and [fs::flags::CLOEXEC] are used when opening -// the file. If you pass your own flags, it is recommended that you add these -// unless you know that you do not want them. -export fn dirfdopen(fd: int, resolve: resolve_flags...) *fs::fs = { - let ofs = alloc(os_filesystem { ... }); - let fs = static_dirfdopen(fd, ofs); - for (let i = 0z; i < len(resolve); i += 1) { - ofs.resolve |= resolve[i]; - }; - fs.close = &fs_close; - return fs; -}; - // Clones a dirfd filesystem, optionally adding additional [resolve_flags] // constraints. export fn dirfs_clone(fs: *fs::fs, resolve: resolve_flags...) *fs::fs = { diff --git a/os/+linux/fdstream.ha b/os/+linux/fdstream.ha @@ -8,6 +8,14 @@ type fd_stream = struct { 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 { ... }); + static_fdopen(fd, strings::dup(name), mode, stream); + stream.stream.closer = &fd_close; + return &stream.stream; +}; + fn static_fdopen( fd: int, name: str, mode: io::mode, stream: *fd_stream, ) *io::stream = { @@ -30,14 +38,6 @@ fn static_fdopen( return &stream.stream; }; -// 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 { ... }); - static_fdopen(fd, strings::dup(name), mode, stream); - stream.stream.closer = &fd_close; - return &stream.stream; -}; - fn is_fdstream(s: *io::stream) bool = { return s.reader == &fd_read || s.writer == &fd_write