pub enum StreamSource<'a> {
Contiguous(&'a [u8]),
Split {
base: &'a [u8],
staged: &'a [u8],
},
}Expand description
Where a ByteSpan carried by a stream in some object graph resolves
to actual bytes.
Two variants, because pdfcer has exactly two kinds of readable document (module docs): a loaded file, whose spans all index one buffer, and an editing session, whose spans index the base file or the R45 staging buffer under one combined coordinate system.
Copy, because it is a pair of shared borrows and threading it through
a render walk by value is cheaper and clearer than by reference.
§Examples
use pdfcer_model::span::ByteSpan;
use pdfcer_model::view::StreamSource;
let base = b"BASE-BYTES";
let staged = b"STAGED";
let src = StreamSource::Split { base, staged };
// A base-side span reads the base half.
assert_eq!(src.slice(ByteSpan::new(0, 4)), Some(&b"BASE"[..]));
// A staged span is expressed as `base.len() + local` (R45).
assert_eq!(src.slice(ByteSpan::new(base.len(), 6)), Some(&b"STAGED"[..]));
// A span that crosses the boundary is a provenance bug: refused.
assert_eq!(src.slice(ByteSpan::new(base.len() - 1, 3)), None);Variants§
Contiguous(&'a [u8])
One buffer: a plain Document’s
retained file bytes. Spans index it directly, exactly as
ByteSpan::slice does.
Split
Two disjoint buffers under one coordinate system: an
EditSession’s base file plus its R45
staging buffer.
A span with start < base.len() belongs to base; a span with
start >= base.len() belongs to staged at local offset
start - base.len(). See the module docs for why a span can never
belong to both.
Implementations§
Source§impl<'a> StreamSource<'a>
impl<'a> StreamSource<'a>
Sourcepub fn slice(&self, span: ByteSpan) -> Option<&'a [u8]>
pub fn slice(&self, span: ByteSpan) -> Option<&'a [u8]>
The bytes span covers, or None if it cannot be served.
None has exactly three causes, all of them logic errors rather
than data errors, and all of them degraded rather than panicked per
the crate’s panic-free policy:
- the span is out of bounds for the buffer it names;
- the span straddles the base/staging boundary (module docs — structurally impossible today, refused so that it stays that way);
- the span was produced against a different document’s buffer.
Callers turn None into their own named refusal — the renderer
counts it as an undecodable stream, pageops stages an empty
payload — rather than substituting plausible bytes.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Total addressable length of this source, in the span coordinate system.
Diagnostic only — used by DocumentView’s Debug and by tests
asserting the R45 offset scheme (see EditSession::stage_bytes). Deliberately NOT a way to get at a
buffer: there is no accessor returning the concatenation, because
materializing one is precisely the cost this type exists to avoid.