-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add AsyncBytes, AsyncLineSequence, AsyncCharacterSequence, and AsyncUnicodeScalarSequence #3036
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
base: main
Are you sure you want to change the base?
Conversation
…nicodeScalarSequence
let fh = try await IOActor.default.createFileHandle(reading: url).bytes.makeAsyncIterator() | ||
buf = fh.buffer | ||
} else { | ||
// TODO: add networking support |
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.
For future reference: use _NSNonfileURLContentLoader
in SCF to load using URLSession if available. Right now it only returns an in-memory NSData
after all is done, but it may be sufficient here.
@swift-ci please test |
public struct Iterator: AsyncIteratorProtocol { | ||
|
||
@inline(__always) static var bufferSize: Int { | ||
16384 |
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.
It occurs to me this should probably be 16384 minus the size of the object header
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.
Or maybe not so the loop can be aligned. Ugh, I'll need to dig into this at some point.
Will follow up on _read after Co-authored-by: Aura Lily Vulcano <[email protected]>
@swift-ci please test |
I missed the change. |
@swift-ci please test |
Ah, never mind, this has merge conflicts. |
@swift-ci please test |
1 similar comment
@swift-ci please test |
@swift-ci please test |
@swift-ci please test |
Waiting for Linux build to pass before correcting the availability annotation for macOS. (Or fail.) |
("testSeparatorPrefixAndContinuationWithoutFin", test_seperator_prefix_and_continuation_without_fin) | ||
] | ||
|
||
return tests |
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.
@Catfish-Man Is this what you meant here? This method was missing its termination.
@swift-ci please test |
@swift-ci please test |
@swift-ci please test |
A little worried about this patch as we haven't even gotten to making tests run yet. @Catfish-Man do you have some time to shepherd it actively? |
(Hopefully this one is the run that gets to unit tests.) |
The tests aren't running because they weren't listed in the main. |
@swift-ci please test |
@swift-ci please test |
#if canImport(Darwin) | ||
let read = Darwin.read | ||
#elseif canImport(Glibc) | ||
let read = Glibc.read | ||
#elseif canImport(CRT) | ||
let read = CRT._read | ||
#endif |
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 there any utility in s-c-f for avoiding doing this everywhere read(2)
is used? If not, is there a specific reason otherwise? (Offhand, "want to just use System
when possible someday" comes to mind.)
Citation: https://github.com/apple/swift-nio/blob/main/Sources/NIOPosix/System.swift#L60-L80 et al
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.
System is a package, which means we would have to lower it into the distribution to use it ourselves :(
@swift-ci please test |
I understand that this pull request is holding up the implementation of the new concurrency APIs in |
@@ -124,6 +124,7 @@ var allTestCases = [ | |||
testCase(TestUnitVolume.allTests), | |||
testCase(TestNSLock.allTests), | |||
testCase(TestNSSortDescriptor.allTests), | |||
testCase(TestFileHandleAsync.allTests), |
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.
(Let me leave a comment before the test logs would be gone.)
Build failure here:
error: invalid conversion from 'async' function of type '() async throws -> ()' to synchronous function type '() throws -> Void'
@YOCKOW so it looks like there just needs to be a utility function written to wrap an async test case since the current implementation only takes non-async functions. Is that correct? |
I think, unfortunately, no. It is used only when |
Alright, so then we need to PR an async version of |
Or, we can use |
Is there anything that a novice community member like me can do to help land this? We're building Lambdas on AWS in Swift and would love to be able to get full AsyncBytes support. |
@swift-ci please test |
The Swift Async Algorithms package has an updated version of this as AsyncBufferedByteIterator. |
@parkera thanks for the link, that should help at least bridge the gap a little - however, it would still be incredibly nice to be able to land this |
// this is not incredibly effecient but it is the best we have | ||
guard let data = try handle.read(upToCount: buffer.count) else { | ||
return 0 | ||
} | ||
data.copyBytes(to: buffer) | ||
return data.count |
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 there a specific reason, handle.readabilityHandler
isn't used here in conjunction with withUnsafeThrowingContinuation
? Since this is still not part of Foundation on Linux, I wanted to include this code in my package for the moment and just wondered if it was better to use readabilityHandler
because this function is blocking otherwise.
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.
readabilityHandler is less useful than you'd hope for regular files. They're just always readable.
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.
But a FileHandle can also be the output of Process, and it may need to wait for new bytes. So the thread will be locked with read(upToCount:)
. I think readabilityHandler
will be a better option that we can wrap into continuation
continue | ||
case _LF..<_CR: | ||
guard let result = yield() else { | ||
continue |
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.
Forgive me if I'm oversimplifying... there's a lot going on here. 😊 It looks like this completely removes empty lines, which seems suboptimal, given that...
- blank lines are meaningful in countless data applications, and
- it's easy enough to add your own
isEmpty
filter on the sequence if you really do want to strip them out.
Based on this, the default behavior should probably be to include empty lines (i.e., change continue
to return ""
in the _CR
and _LF
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.
If in doubt, please cross-check with Foundation's behavior. It needs to have the same semantics, else we widen the (already large) gap between APPLE and !APPLE.
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.
Ah, yeah, good point. This does match Foundation's behavior, so the issue would be against Apple, not this code. Thanks for the check!
Only the local-file versions of AsyncBytes for now