Skip to content

Add span-based input and output interfaces#1816

Merged
kperryua merged 1 commit intoswiftlang:experimental/new-codablefrom
kperryua:span-based-input-output-interfaces
Mar 16, 2026
Merged

Add span-based input and output interfaces#1816
kperryua merged 1 commit intoswiftlang:experimental/new-codablefrom
kperryua:span-based-input-output-interfaces

Conversation

@kperryua
Copy link
Copy Markdown
Contributor

The Data interfaces still exist for convenience. They should eventually move to cross-module imports.

@kperryua kperryua added enhancement New feature or request new-codable Related to new Swift (de)serialization APIs labels Mar 13, 2026
@inlinable
public func decode<T: CommonDecodable>(_ type: T.Type, from data: Data) throws(CodingError.Decoding) -> T {
try _decode(type, from: data) { parserDecoder throws(CodingError.Decoding) in
public func decode<T: CommonDecodable>(_ type: T.Type, from span: Span<UInt8>) throws(CodingError.Decoding) -> T {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be RawSpan?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely clear on that TBH. For UTF-8 input, we very clearly are semantically reading individual bytes, which makes Span<UInt8> make more sense to be as input. However, internally we're holding on to it as a RawSpan, because we can use SIMD/SWAR techniques to read multiple bytes at a time.

That said, A) it's more straightforward for clients to get a RawSpan from a Span<UInt8> than to go the other way, and B) eventually I suspect we will need to support other wider Unicode encodings like present-day JSONDecoder does, which means we're no longer processing the input byte-by-byte.

@kperryua kperryua linked an issue Mar 13, 2026 that may be closed by this pull request
@kperryua kperryua force-pushed the span-based-input-output-interfaces branch from dedb966 to cf2e842 Compare March 14, 2026 00:03
@kperryua
Copy link
Copy Markdown
Contributor Author

Just the known Windows crash on the tests. @t089, any comments on this?

Copy link
Copy Markdown

@t089 t089 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

@kperryua kperryua merged commit 2290571 into swiftlang:experimental/new-codable Mar 16, 2026
36 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request new-codable Related to new Swift (de)serialization APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NewJSONDecoder needs span-taking decode functions

2 participants