Skip to content
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

Add read/write specialisation for IOContext{AnnIO} #53715

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,24 @@ function write(dest::AnnotatedIOBuffer, src::AnnotatedIOBuffer)
nb
end

# So that read/writes with `IOContext` (and any similar `AbstractPipe` wrappers)
# work as expected.
Copy link
Contributor

Choose a reason for hiding this comment

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

Still trying to wrap my brain around this:

  • by default, unsafe_write(io::AbstractPipe, ...) for simply redispatches to unsafe_write(pipe_writer(io)::IO, ...)
  • by default, all write(io, ...) methods in the end go through unsafe_write(io, ...)
  • so naively write(pipe_writer(io), s) seems pointless
  • but then there are custom write methods ... such as your write(io::AnnotatedIOBuffer, astr::Union{AnnotatedString, SubString{<:AnnotatedString}}) method...

And now we have a problem if AbstractPipe type, such as IOContext, wraps an AnnotatedIOBuffer: instead of dispatching from write(io, ...) to unsafe_write(io, ...) you need it to go through pipe_writer(io) so that the annotations can be handled by that.

What if there is a sandwich of three AbstractPipe types, say an IOContext outermost, then some other AbstractPipe type, then the AnnotatedIOBuffer -- it wouldn't work out then either, right?

This then makes me wonder: why not "simply" something like

write(io::AbstractPipe, s::AnnotatedString) = write(pipe_writer(io), s)

I bet you considered this and ruled it out for some reason, but I didn't see it in the PR discussion.

None of this is meant as fundamental objection to this PR, but I feel at least this comment should be a bit more elaborate (which, if you happen to agree with this POV, could certainly wait for a future PR -- I don't mind to hold this one up). I'd love to be helpful and suggest something, but for that I'd first have to really understand it, hence my questions :-).

function write(io::AbstractPipe, s::Union{AnnotatedString, SubString{<:AnnotatedString}})
if pipe_writer(io) isa AnnotatedIOBuffer
write(pipe_writer(io), s)
else
invoke(write, Tuple{IO, typeof(s)}, io, s)
end::Int
end
# Can't be part of the `Union` above because it introduces method ambiguities
function write(io::AbstractPipe, c::AnnotatedChar)
if pipe_writer(io) isa AnnotatedIOBuffer
write(pipe_writer(io), c)
else
invoke(write, Tuple{IO, typeof(c)}, io, c)
end::Int
end
Comment on lines +505 to +519
Copy link
Contributor

Choose a reason for hiding this comment

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

The following is a 100% optional suggestion that is ultimately a matter of taste

As you are most likely aware, you could avoid the code duplication by using @eval, i.e.

Suggested change
function write(io::AbstractPipe, s::Union{AnnotatedString, SubString{<:AnnotatedString}})
if pipe_writer(io) isa AnnotatedIOBuffer
write(pipe_writer(io), s)
else
invoke(write, Tuple{IO, typeof(s)}, io, s)
end::Int
end
# Can't be part of the `Union` above because it introduces method ambiguities
function write(io::AbstractPipe, c::AnnotatedChar)
if pipe_writer(io) isa AnnotatedIOBuffer
write(pipe_writer(io), c)
else
invoke(write, Tuple{IO, typeof(c)}, io, c)
end::Int
end
# (can't do this via a single method with a 'Union' argument due to method ambiguities)
for T in (AnnotatedChar, AnnotatedString, SubString{<:AnnotatedString}) do
@eval function write(io::AbstractPipe, arg::$T)
if pipe_writer(io) isa AnnotatedIOBuffer
write(pipe_writer(io), arg)
else
invoke(write, Tuple{IO, typeof(arg)}, io, arg)
end::Int
end
end


"""
_clear_annotations_in_region!(annotations::Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}, span::UnitRange{Int})

Expand Down
7 changes: 7 additions & 0 deletions test/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,11 @@ end
@test write(aio2, Base.AnnotatedChar('c', [:b => 2, :c => 3, :d => 4])) == 1
@test Base.annotations(aio2) == [(1:2, :a => 1), (1:3, :b => 2), (3:3, :c => 3), (3:3, :d => 4)]
end
# Working through an IOContext
aio = Base.AnnotatedIOBuffer()
wrapio = IOContext(aio)
@test write(wrapio, Base.AnnotatedString("hey", [(1:3, :x => 1)])) == 3
@test write(wrapio, Base.AnnotatedChar('a', [:y => 2])) == 1
@test read(seekstart(aio), Base.AnnotatedString) ==
Base.AnnotatedString("heya", [(1:3, :x => 1), (4:4, :y => 2)])
end