pub struct ByteSpan {
pub start: usize,
pub len: usize,
}Expand description
A half-open byte range [start, start + len) into a retained source
buffer.
See the module docs for why this type is load-bearing rather than a
convenience. It is deliberately a plain value type (Copy, ordered,
hashable) so it can be embedded in every token and object without
ceremony.
§Examples
use pdfcer_model::span::ByteSpan;
let buf = b"12 0 obj";
let span = ByteSpan::new(0, 2); // the "12" token
assert_eq!(span.slice(buf), Some(&b"12"[..]));
assert_eq!(span.end(), 2);Fields§
§start: usizeOffset of the first byte, from byte 0 of the retained buffer.
len: usizeNumber of bytes covered. May be 0 (e.g. the empty name /
has a 1-byte span for its solidus, but an empty decoded value —
zero-length spans arise in synthesized entities).
Implementations§
Source§impl ByteSpan
impl ByteSpan
Sourcepub const fn new(start: usize, len: usize) -> Self
pub const fn new(start: usize, len: usize) -> Self
Construct a span from a start offset and length.
Sourcepub const fn from_range(range: Range<usize>) -> Self
pub const fn from_range(range: Range<usize>) -> Self
Construct a span covering range (start..end).
Returns a zero-length span at range.start if the range is
inverted (end < start) — inverted ranges are always a caller
bug, but this type is used in paths that must not panic
(pdfcer-core’s crate-level panic-free policy), so it degrades to
the least-harmful value instead.
Sourcepub fn slice(self, buf: &[u8]) -> Option<&[u8]>
pub fn slice(self, buf: &[u8]) -> Option<&[u8]>
The bytes this span covers in buf, or None if the span is out
of bounds for buf.
None here always indicates a logic error (a span applied to a
buffer it wasn’t produced from); it is surfaced as an Option
rather than a panic per the crate’s panic-free policy, so callers
convert it into a proper structural error.