-
Notifications
You must be signed in to change notification settings - Fork 92
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
Fixes #35: fd{Read,Write} with ByteString payload #186
Conversation
This commit addes two new functions: fdReadBytes and fdWriteBytes. They are like fdRead and fdWrite respectively but instead of reading/writing String they use ByteStrings.
@@ -1,7 +1,7 @@ | |||
{-# LANGUAGE CApiFFI #-} | |||
{-# LANGUAGE NondecreasingIndentation #-} | |||
{-# LANGUAGE RecordWildCards #-} | |||
{-# LANGUAGE Safe #-} | |||
{-# LANGUAGE Trustworthy #-} |
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.
Is this incidental cleanup, or "essential" to this PR? To be honest I've never taken the time to understand Safe
, Trustworthy
, ... So I don't know whether this change is correct or belongs in this PR (collateral cleanup is of course OK in some cases).
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.
Yes, it is need; Otherwise we cannot safely import that module in System.Posix.IO.ByteString.
-- reached. | ||
fdReadBytes :: Fd | ||
-> ByteCount -- ^How many bytes to read | ||
-> IO (ByteString, ByteCount) -- ^The bytes read, how many bytes were read. |
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.
ByteStrings
already carry a length field, there's little point in returning it another copy wrapped in a tuple. This should use createUptoN
and return just the ByteString
.
Also, not all errors are invalid descriptor or EOF
. On a non-blocking file descriptor, the error could be EAGAIN, and fdReadBuf
will have thrown the corresponding IO Exception.
I am not sure that raising on EOF
is the most useful result, returning an empty ByteString may be a cleaner choice, but perhaps we're too used to EOF exceptions in Haskell... :-(
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.
Also, not all errors are invalid descriptor or
EOF
. On a non-blocking file descriptor, the error could be EAGAIN, andfdReadBuf
will have thrown the corresponding IO Exception.
I am not sure that raising onEOF
is the most useful result, returning an empty ByteString may be a cleaner choice, but perhaps we're too used to EOF exceptions in Haskell... :-(
Not sure yet what my opinion is here; I tried to stick to the existing design (with all its bugs and quirks) here. If we decide to change the way how we handle EOF I suggest we do it in #219 and discuss it over there.
My opinion is that these shouldn't be new functions, but that the existing functions
That makes no sense. So I think breaking API here is the right choice. |
@hasufell I wholeheartedly aggree with you but wanted to be careful with backwards-incompatible changes. If no one objects I'll update this PR such that it fixes this bug and remove the new functions. |
@hs-viktor opinions? Users who use the old functions can fix their code easily by importing the ones from System.Posix.IO instead. I think that's a reasonable migration strategy and can likely be automated for large codebases. |
We could also add a test identical to https://github.com/haskell/unix/blob/master/tests/Posix014.hs |
I opened another PR #219 implementing the fix like @hasufell suggested in #186 (comment). |
@hasufell Will add one once we decide which PR will make it. |
On the surface level this is kinda expected, the contract is that unix/System/Posix/ByteString.hs Lines 16 to 21 in e919c7d
However, the underlying rationale for the existence of |
Dropped in favor of #219 |
This commit addes two new functions: fdReadBytes and fdWriteBytes.
They are like fdRead and fdWrite respectively but instead of
reading/writing String they use ByteStrings.
Fixes #35