-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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::Delimited #2973
Add IO::Delimited #2973
Conversation
d4051d9
to
a1146cc
Compare
def read(slice : Bytes) | ||
return 0 if @slice.size == 0 | ||
max_read_size = {slice.size, @slice.size}.min | ||
# read_size = {1, max_read_size / 2}.max |
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.
Leftover comment :)
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.
Removed!
a1146cc
to
95914eb
Compare
@@ -239,6 +239,68 @@ struct Slice(T) | |||
pointer(count).copy_to(target, count) | |||
end | |||
|
|||
# Copies the contents of this slice into *target*. | |||
# | |||
# Truncates if this slice doesn't fit into the destination slice. |
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 don't think silently truncating is good. If there's not enough room it should raise.
Are you using this "feature" in your code?
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.
The point of the API is that it copies the size of the smaller slice, and returns the amount of bytes it copies. I think this API is sane, but i'm not sure, i'm willing to be convinced either way.
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.
And yes, i'm using it here.
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.
From IRC:
<RX14> i don't want it to throw
<jhass> but given all the slice stuff raises on bounds related things
<jhass> it probably should remain a callside responsibility for now
<RX14> no it raises when you try and ask it to do something stupid explicitly
<RX14> that doesn't set a precedent for copying to raise if the user hasn't truncated the slices right
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 fixed this: It raises when src will not fit into dest, but not when src does not completely fill dest.
7d513c0
to
f76b83e
Compare
f76b83e
to
e765ab9
Compare
Just fixed the style violation, anything else needed before merging? |
@RX14 Looks good now. One last thing: maybe |
@asterite well i kinda left it like that in case we ever wanted to add a write version, and the |
You mean, you'd write until you find a delimiter being written? Well, in any case |
@asterite those were my thoughts exactly |
@RX14 Thank you! |
From docs:
Also adds some convenience methods to
Slice
that are (or were) utilised in implementingIO::Delimited
.The algorithm for
IO::Delimited
is a little complex, but I believe it's readable with the docs and comments I've added. It's also reasonably performant: benchmarks. It seems to be able to do about 1gbps/core, which should be enough for most usages.