hare

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

commit 10eac30bd581236ab6f7f0082fe4e69ba6181035
parent 2fc75eeeddf7dcc4688221616c72da18c69072fc
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 31 Jan 2021 15:20:52 -0500

Update existing code to use named struct initializers

Diffstat:
Mos/+linux/fdstream.ha | 32+++++++++++++++-----------------
Mstrings/cstrings.ha | 10++++++----
Mstrings/utf8.ha | 8++++----
3 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/os/+linux/fdstream.ha b/os/+linux/fdstream.ha @@ -1,5 +1,6 @@ use io; use rt; +use strings; type fd_stream = struct { stream: io::stream, @@ -9,19 +10,16 @@ type fd_stream = struct { // Opens a Unix file descriptor as an io::stream. If 'name' is an empty string, // a name will be generated based on the file descriptor number. export fn fdopen(fd: int, name: str) *io::stream = { - // TODO: strings::dup the name, and if empty, generate a name from the - // file descriptor number. - // - // Also TODO: consider making the caller specify what subset of - // operations should be supported - let stream = alloc(*fd_stream, struct { - stream: io::stream = struct { - name: str = name, - reader: nullable *io::reader = &fd_read, - writer: nullable *io::writer = &fd_write, - closer: nullable *io::closer = &fd_close, + // TODO: consider making the caller specify what subset of operations + // should be supported + let stream = alloc(*fd_stream, fd_stream { + stream = io::stream { + name = strings::dup(name), + reader = &fd_read, + writer = &fd_write, + closer = &fd_close, }, - fd: int = fd, + fd = fd, }); return &stream.stream; }; @@ -57,11 +55,11 @@ fn fd_close(s: *io::stream) void = { }; fn static_fdopen(fd: int, name: str, stream: *fd_stream) void = { - stream.stream = struct { - name: str = name, - reader: nullable *io::reader = &fd_read, - writer: nullable *io::writer = &fd_write, - closer: nullable *io::closer = &fd_close, + stream.stream = io::stream { + name = name, + reader = &fd_read, + writer = &fd_write, + closer = &fd_close, }; stream.fd = fd; }; diff --git a/strings/cstrings.ha b/strings/cstrings.ha @@ -1,3 +1,5 @@ +use types; + // Computes the length of a NUL-terminated C string, in octets, in O(n). The // computed length not include the NUL terminator in the length. export fn c_strlen(cstr: *const char) size = { @@ -10,10 +12,10 @@ export fn c_strlen(cstr: *const char) size = { // Converts a C string to a Hare string in O(n). export fn from_c(cstr: *const char) const str = { const l = c_strlen(cstr); - const s = struct { // TODO: Use types::string - data: *[*]u8 = cstr: *[*]u8, - length: size = l, - capacity: size = l, + const s = types::string { + data = cstr: *[*]u8, + length = l, + capacity = l, }; return *(&s: *const str); }; diff --git a/strings/utf8.ha b/strings/utf8.ha @@ -3,10 +3,10 @@ use types; // Converts a byte slice into a string WITHOUT checking that the byte slice is a // valid UTF-8 string. export fn from_utf8_unsafe(in: []u8) str = { - const s = struct { // TODO: Use types::string - data: *[*]u8 = (&in: *types::slice).data: *[*]u8, - length: size = len(in), - capacity: size = len(in), + const s = types::string { + data = (&in: *types::slice).data: *[*]u8, + length = len(in), + capacity = len(in), }; return *(&s: *const str); };