Skip to main content

ObjectGraph

Trait ObjectGraph 

Source
pub trait ObjectGraph: Send + Sync {
    // Required methods
    fn value(&self, id: ObjId) -> Option<&Object>;
    fn trailer_entry(&self, key: &[u8]) -> Option<&Object>;

    // Provided methods
    fn stream_raw(&self, stream: &Stream) -> Option<&[u8]> { ... }
    fn resolve<'a>(&'a self, obj: &'a Object) -> &'a Object { ... }
    fn resolved(&self, id: ObjId) -> &Object { ... }
    fn catalog_dict(&self) -> Option<&Dict> { ... }
    fn catalog_id(&self) -> Option<ObjId> { ... }
}
Expand description

A read-only view of one PDF document’s object graph.

Implemented by Document (the file as loaded) and by EditSession’s overlay view (the file as the operator currently has it). See the module docs for why this exists at all.

§Examples

use pdfcer_model::document::Document;
use pdfcer_model::graph::ObjectGraph;

let doc = Document::from_bytes(
    include_bytes!("../../../fixtures/synthetic/hello.pdf").to_vec(),
)?;
// The catalog is reached through the trailer's /Root, whichever
// view is doing the reaching.
assert!(doc.catalog_dict().is_some());

§Thread safety — why this trait requires Send + Sync

The bound exists so a page can be rasterized off the UI thread. A GUI that rasterizes inline freezes for the length of the render, and on a real CAD sheet that is ~10 s at 1× and ~58 s at 2× — not a slow redraw but a dead application, with no repaint, no progress and no way to cancel. Moving that work to a worker requires the rendered value to cross a thread boundary, and DocumentView is what crosses.

Only the trait object was ever the obstacle. Document, EditSession, SessionGraph, PendingGraph and the test graphs were all already thread-safe; DocumentView<'a> holds &'a dyn ObjectGraph, and an unbounded dyn Trait is neither Send nor Sync regardless of what implements it. Adding the supertrait compiled with zero errors across the workspace — it records a property every implementor already had rather than imposing a new one.

Why now. Adding a supertrait bound to a public trait is a breaking change for downstream implementors. pdfcer has no remote and no release (LEGAL.md rule 8 — publishing awaits an explicit go-ahead), so the set of affected implementors is exactly the ones in this repository, and it is empty. This is the cheapest moment this bound will ever have.

The cost, stated. Any future ObjectGraph implementor must be thread-safe. That forecloses one holding an Rc, a RefCell, or a non-thread-safe foreign handle. Rust API guideline C-SEND-SYNC asks for types to be Send/Sync where possible and for deliberate exceptions to be documented; an object graph is plain data behind an accessor, so an implementor that could not meet this would be the surprising one.

Required Methods§

Source

fn value(&self, id: ObjId) -> Option<&Object>

The current value of id, or None when this view has no such object.

“No such object” folds together every §7.3.10/§7.5.4 way an id can fail to resolve — never defined, freed, or named with a stale generation — because they are indistinguishable to a reader and the spec gives them one outcome.

Source

fn trailer_entry(&self, key: &[u8]) -> Option<&Object>

The current value of the trailer entry key, or None.

Only /Root is consulted by anything in this crate today, but the accessor is general because an overlay view must be able to answer for a trailer the operator has patched (creating /Info is already such a patch, from Pass 3.1).

Provided Methods§

Source

fn stream_raw(&self, stream: &Stream) -> Option<&[u8]>

The raw (still-encoded) data of stream, an object of this graph, or None when this graph cannot serve stream bytes.

The default is None: a graph of values alone has no byte source. crate::document::Document, crate::view::DocumentView and an editing session override it, so a walk generic over the graph can read a value the format allows as either a string or a stream (a §12.7.3.4 /RV text stream).

Source

fn resolve<'a>(&'a self, obj: &'a Object) -> &'a Object

Follow a reference chain to a non-reference value, applying §7.3.10’s rules: an unresolvable reference yields null rather than an error, and the chain is depth-guarded (MAX_RESOLVE_DEPTH) so 5 0 obj 5 0 R endobj — legal syntax — cannot loop.

Source

fn resolved(&self, id: ObjId) -> &Object

Resolve id directly to a non-reference value, or null.

Convenience for the very common “I have an id, give me the dictionary” shape, which would otherwise need a temporary Object::Reference to hand to ObjectGraph::resolve — and a temporary cannot outlive the call, so the borrow does not compile.

Iterative, not recursive, and depth-guarded for the same reason ObjectGraph::resolve is: 4 0 obj 4 0 R endobj is legal syntax, and a recursive implementation would answer it with a stack overflow — which pdfcer-core’s panic-free policy forbids on untrusted input just as firmly as it forbids an unwrap.

Source

fn catalog_dict(&self) -> Option<&Dict>

The document catalog (§7.7.2) — the dictionary the trailer’s /Root points at — or None if the file has no usable one.

Returns Option rather than Result because every caller in the structural-operations path already has its own error type and maps this to a variant of it; a second error type in the middle would only be unwrapped and rewrapped.

Source

fn catalog_id(&self) -> Option<ObjId>

The object id the trailer’s /Root names, if it is a reference.

Needed separately from ObjectGraph::catalog_dict by anything that must write the catalog back (structural operations all do), because a dictionary borrowed out of the graph carries no record of which object it came from.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl ObjectGraph for Document

The loaded file, as a graph.

Document already has inherent resolve/catalog methods with the same semantics; those keep working and keep winning method resolution (inherent methods are preferred), so this impl adds a generic entry point without changing a single existing call site.

Source§

impl ObjectGraph for DocumentView<'_>

Delegating impl — the mechanism that let decision 018 change 27 function signatures without touching 45 call-site bodies (type docs).