Skip to content

Commit

Permalink
Resolve transcode method ambiguity
Browse files Browse the repository at this point in the history
The examples in many of the codec packages rely on being able to call
`transcode` providing a `Codec` type and a string[^1][^2]. JuliaIO#136 removed
type annotations from the trailing arguments for `transcode(::Type{C},
...) where {C<:Codec}` causing a method ambiguity with `transcode(T,
src::String)` in Julia `Base`[^3]. This adds an additional method to
`Base.transcode` to resolve this ambiguity.

Fixes JuliaIO#139.

[^1]: https://github.com/JuliaIO/CodecZlib.jl/tree/f9fddaa28c093c590a7a93358709df2945306bc7#usage
[^2]: https://github.com/JuliaIO/CodecZstd.jl/tree/6327ffa9a3a12fc46d465dcfc8b30bed91cf284b#usage
[^3]: https://github.com/JuliaLang/julia/blob/ff7b8eb00bf887f20bf57fb7e53be0070a242c07/base/c.jl#L306
  • Loading branch information
bauglir committed Apr 15, 2023
1 parent 3583abe commit 642db71
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/transcode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function Base.transcode(::Type{C}, args...) where {C<:Codec}
end
end

# Disambiguate `Base.transcode(::Type{C}, args...)` above from
# `Base.transcode(T, ::String)` in Julia `Base`
function Base.transcode(codec::Type{C}, src::String) where {C<:Codec}
return invoke(transcode, Tuple{Any, String}, codec, src)
end

_default_output_buffer(codec, input) = Buffer(
initial_output_size(
codec,
Expand Down
5 changes: 5 additions & 0 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
output = TranscodingStreams.Buffer(Vector{UInt8}())
@test transcode(Noop(), data, output) === output.data

data = ""
@test String(transcode(Noop, data)) == data
data = "foo"
@test String(transcode(Noop, data)) == data

TranscodingStreams.test_roundtrip_transcode(Noop, Noop)
TranscodingStreams.test_roundtrip_read(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_write(NoopStream, NoopStream)
Expand Down

0 comments on commit 642db71

Please sign in to comment.