Skip to content

Commit

Permalink
Add read/write specialisation for IOContext{AnnIO}
Browse files Browse the repository at this point in the history
Ensure that when an AnnotatedIOBuffer is wrapped in an IOContext, that
writes of annotated strings/chars and reading out an AnnotatedString is
unimpeded by the IOContext wrapping.

Without these specialisations, the generic pipe_reader/pipe_writer
fallbacks will directly access the underlying IOBuffer and annotations
will be lost.

There are a number of scenarios in which one might want to combine an
AnnotatedIOBuffer and IOContext (for example setting the compact
property). Losing annotations in such scenarios is highly undesirable.

The read specialisation isn't actually needed to avoid this headache,
but seems nice to have for completeness.
  • Loading branch information
tecosaur committed Mar 12, 2024
1 parent 2a72d65 commit d0b11b0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ function show_circular(io::IOContext, @nospecialize(x))
return false
end

# Specialisations that would be put in `annotated.jl`, but for the fact it's loaded first.
write(io::IOContext{AnnotatedIOBuffer}, s::Union{AnnotatedString, SubString{<:AnnotatedString}}) =
write(io.io, s)
write(io::AnnotatedIOBuffer, c::AnnotatedChar) = write(io.io, c)
read(io::IOContext{AnnotatedIOBuffer}, T::Type) = read(io.io, T)

"""
show([io::IO = stdout], x)
Expand Down

0 comments on commit d0b11b0

Please sign in to comment.