-
Notifications
You must be signed in to change notification settings - Fork 109
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
[Pitch] Add non-throwing FileDescriptor.currentOffset helper #50
Open
sharplet
wants to merge
1
commit into
apple:main
Choose a base branch
from
sharplet:current-offset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 about for files that have no concept of position?
That being said, I do like the idea of a very simple AEIC helper.
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.
Great point, I must have missed that one in my first pass. Here’s a review of the error codes on Darwin and how we might handle each:
EBADF
: Implies that the instance was constructed with an invalid descriptor usinginit(rawValue:)
, or is being called afterclose()
, or something else. I think it makes sense to trap here.EINVAL
: The seek location andwhence
aren’t provided by the client. This would presumably be a bug in System, and should trap.ENXIO
: Only applies toSEEK_DATA
andSEEK_HOLE
.EOVERFLOW
: File is larger than would fit in anInt64
. Should probably trap?ESPIPE
: No concept of a current location. This seems much more likely than the other cases, and so a trap here might be more surprising to the caller.According to https://man7.org/linux/man-pages/man2/lseek.2.html, calling
lseek(2)
on a terminal device will fail withESPIPE
. I just tested this on Darwin withstdin
, and it doesn’t fail, although the return value is probably meaningless.My first inclination was to change the return type to
Int64?
. We could still trap for the above documented cases, but onESPIPE
returnnil
. It’s already up to the user to understand what kind of file descriptor they’re working with, which they could use to decide whether to force unwrap. However, a given file descriptor is either always going to return a valid offset, or always returnnil
, and being required to force unwrap when you know you have a regular file is unfortunate (although still far more convenient thantry! fd.seek(offset: 0, from: .current)
).Depending on System’s source compatibility story, a second option is a throwing accessor, resulting in
try fd.currentOffset
. This doesn’t discard any information, while providing a convenient shorthand.I think it might boil down to this: one has to know whether this method is valid to call on a given file descriptor already. Given the API options I’ve described so far, all of the following would trap:
fd.currentOffset
,fd.currentOffset!
ortry! fd.currentOffset
.If we can require Swift 5.5, I think I’m most in favour of the last one. I don’t think the optional version of the API is enough of an improvement over the current patch. If requiring Swift 5.5 isn’t an option, maybe we could make it a method instead, or perhaps guard it with an
#if compiler()
directive?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'm also inclined towards the throwing computed var. I'll have to check how to deal with the required tools versions.