-
-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add io stream primitives #1626
Add io stream primitives #1626
Conversation
Implement a MultiReader (InStream) which sequentially read from the provided readers (InStreams). Return IoError.EOF when all of the readers are read.
Implement a MultiWriter (OutStream). The MultiWriter duplicates its writes to all the provided writers (OutStream).
Implement a TeeReader (InStream) which reads from a wrapped reader (InStream) and writes data to the provided writer (OutStream).
Ugh, sorry. All tests failed. After a cursory look it's not obvious to me why it failed. I'll have to look into this. |
lib/std/io/io.c3
Outdated
@@ -42,6 +42,7 @@ fault IoError | |||
UNKNOWN_ERROR, | |||
UNSUPPORTED_OPERATION, | |||
WOULD_BLOCK, | |||
SHORT_WRITE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between this one an "INCOMPLETE_WRITE"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have overlooked INCOMPLETE_WRITE. That expresses the intent of short write error. I'll adjust this and hope that the build error goes away.
lib/std/io/stream/multireader.c3
Outdated
fn MultiReader* MultiReader.new_init(&self, InStream... readers, Allocator allocator = allocator::heap()) | ||
{ | ||
usz n = readers.len; | ||
InStream *ptr = allocator::new_array(allocator, InStream, n); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allocator::new_array
already returns a slice, so you can do:
InStream[] copy = allocator::new_array(allocator, InStream, readers.len);
copy[..] = readers[..];
*self = { .readers = copy, .allocator = allocator };
return self;
lib/std/io/stream/multireader.c3
Outdated
fn void MultiReader.free(&self) | ||
{ | ||
if (!self.allocator) return; | ||
if (void* ptr = self.readers.ptr) allocator::free(self.allocator, ptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This:
if (void* ptr = self.readers.ptr) allocator::free(self.allocator, ptr);
Can be replaced by:
allocator::free(self.allocator, self.readers);
lib/std/io/stream/multiwriter.c3
Outdated
fn MultiWriter* MultiWriter.new_init(&self, OutStream... writers, Allocator allocator = allocator::heap()) | ||
{ | ||
usz n = writers.len; | ||
OutStream *ptr = allocator::new_array(allocator, OutStream, n); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same note as for the Reader
lib/std/io/stream/multiwriter.c3
Outdated
foreach (w : self.writers) | ||
{ | ||
n = w.write(bytes)!; | ||
if (n != bytes.len) return IoError.SHORT_WRITE?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
INCOMPLETE_WRITE maybe?
lib/std/io/stream/multiwriter.c3
Outdated
fn void MultiWriter.free(&self) | ||
{ | ||
if (!self.allocator) return; | ||
if (void* ptr = self.writers.ptr) allocator::free(self.allocator, ptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the note on MultiReader.free
lib/std/io/stream/teereader.c3
Outdated
@param [&inout] r "Stream r to read from." | ||
@param [&inout] w "Stream w to write to what it reads from r." | ||
*> | ||
fn TeeReader tee_reader(InStream r, OutStream w) => { r, w }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could possibly be a macro or set to @inline
Probably the added IO fault added the line number by one in a bunch of places and the test scripts are sensitive to that. I can fix it if you want. Did you need the "SHORT_WRITE" when there was INCOMPLETE_WRITE? If you remove SHORT_WRITE then it will probably just work for now. |
Thank you! |
Add io stream primitives for the InStream and OutStream interfaces:
Inspired by Golang's io package.