Skip to main content

Codec

Trait Codec 

Source
pub trait Codec {
    // Required methods
    fn name(&self) -> &'static str;
    fn extensions(&self) -> &'static [&'static str];
    fn read_bytes(&self, bytes: &[u8]) -> Result<Model, ModelError>;
    fn write(
        &self,
        model: &Model,
        out: &mut dyn Write,
    ) -> Result<(), ModelError>;

    // Provided methods
    fn detect(&self, _bytes: &[u8]) -> bool { ... }
    fn read_owned(&self, bytes: Vec<u8>) -> Result<Model, ModelError> { ... }
    fn read_from(&self, reader: &mut dyn Read) -> Result<Model, ModelError> { ... }
    fn read_path(&self, path: &Path) -> Result<Model, ModelError> { ... }
    fn write_path(&self, model: &Model, path: &Path) -> Result<(), ModelError> { ... }
    fn write_bytes(&self, model: &Model) -> Result<Vec<u8>, ModelError> { ... }
}
Expand description

Read and write one concrete IFC serialization.

Implementors are stateless; they carry configuration only.

Required Methods§

Source

fn name(&self) -> &'static str

Human-readable name for diagnostics, e.g. STEP.

Source

fn extensions(&self) -> &'static [&'static str]

Conventional file extensions, lower-case, without the dot.

Source

fn read_bytes(&self, bytes: &[u8]) -> Result<Model, ModelError>

Parse a model from bytes.

Bytes rather than &str because IFC files are not guaranteed UTF-8: STEP escapes non-ASCII text, and a stray raw byte must not abort the parse of an otherwise valid file.

Source

fn write(&self, model: &Model, out: &mut dyn Write) -> Result<(), ModelError>

Serialize a model.

Provided Methods§

Source

fn detect(&self, _bytes: &[u8]) -> bool

Does this look like a file this codec can read?

Content sniffing, so a file with the wrong extension still opens. Defaults to false — a codec that cannot cheaply recognize its own format should say so rather than claim every input, which would make codec selection order-dependent.

Source

fn read_owned(&self, bytes: Vec<u8>) -> Result<Model, ModelError>

Parse a model from a buffer the model may keep.

A codec that loads lazily keeps its source for the model’s lifetime; handing it the buffer avoids copying the whole input once more. Defaults to Codec::read_bytes.

Source

fn read_from(&self, reader: &mut dyn Read) -> Result<Model, ModelError>

Parse from any reader. Override when the format can stream.

Source

fn read_path(&self, path: &Path) -> Result<Model, ModelError>

Parse a file from disk.

Override this when the codec can memory-map instead of reading into a heap buffer; ifc-step does exactly that for large models.

Source

fn write_path(&self, model: &Model, path: &Path) -> Result<(), ModelError>

Serialize to a file on disk.

Source

fn write_bytes(&self, model: &Model) -> Result<Vec<u8>, ModelError>

Serialize to a byte vector.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§