Expand description
Binary wire codec for the SIM runtime.
This crate encodes and decodes kernel Expr values as a compact, tagged
binary frame. A frame begins with a magic/version header and side tables
(interned libs, symbols, and number domains), followed by a tag-prefixed
body that walks the Expr graph; an optional flag carries source origins so
that located expressions and LocatedExprTree values round-trip as well.
The public surface is the BinaryCodec runtime object (registered via
BinaryCodecLib) together with the free encode_* / decode_* frame
functions and the frame value types (BinaryFrame, BinaryTag,
FrameTables, DecodeLimits). Decoding is bounded by DecodeLimits
to fail closed on hostile or malformed input.
§Examples
Encode an Expr into a BinaryFrame and decode it straight back,
recovering both the value and its interning side tables – no runtime
context is needed for the free frame functions:
use sim_codec_binary::{BinaryFrame, decode_frame, encode_frame};
use sim_kernel::{CodecId, Expr, Symbol};
let expr = Expr::List(vec![
Expr::Symbol(Symbol::qualified("math", "add")),
Expr::Bool(true),
]);
let BinaryFrame(bytes) = encode_frame(&expr)?;
let (tables, back) = decode_frame(CodecId(1), &bytes)?;
assert_eq!(back, expr);
assert!(tables.symbols.contains(&Symbol::qualified("math", "add")));Arbitrary bytes are untrusted data, not executable input: a frame that does not start with the magic header fails closed rather than running anything.
use sim_codec_binary::decode_frame;
use sim_kernel::CodecId;
assert!(decode_frame(CodecId(1), b"not a frame").is_err());Register the codec on a runtime and round-trip through the codec surface:
use std::sync::Arc;
use sim_codec::{Input, Output, decode_with_codec, encode_with_codec};
use sim_codec_binary::BinaryCodecLib;
use sim_kernel::{Cx, DefaultFactory, EagerPolicy, Expr, ReadPolicy, Symbol};
let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
sim_test_support::register_core_classes(&mut cx);
let lib = BinaryCodecLib::new(cx.registry_mut().fresh_codec_id());
cx.load_lib(&lib)?;
let binary = Symbol::qualified("codec", "binary");
let bytes = match encode_with_codec(&mut cx, &binary, &Expr::Bool(true), Default::default())? {
Output::Bytes(bytes) => bytes,
Output::Text(text) => text.into_bytes(),
};
let back = decode_with_codec(&mut cx, &binary, Input::Bytes(bytes), ReadPolicy::default())?;
assert_eq!(back, Expr::Bool(true));Structs§
- Binary
Codec - Binary codec runtime object that round-trips kernel
Exprvalues as compact tagged frames. - Binary
Codec Lib Libthat registers the binary codec with the runtime.- Binary
Frame - A complete encoded binary frame: a magic/version header, side tables, and
the tag-prefixed
Exprbody, owned as a single byte buffer. - Decode
Limits - Fail-closed bounds applied while decoding an untrusted binary frame.
- Frame
Tables - The interning side tables carried in a frame header.
Enums§
- Binary
Tag - The one-byte tag that prefixes each node in a frame body.
Functions§
- decode_
frame - Decodes frame
bytesinto its sideFrameTablesand bareExpr. - decode_
located_ frame - Decodes frame
bytesinto its sideFrameTablesand aLocatedExpr. - decode_
located_ tree_ frame - Decodes frame
bytesinto its sideFrameTablesand a fullLocatedExprTree, using the defaultDecodeLimits. - decode_
located_ tree_ frame_ with_ limits - Decodes frame
bytesinto its sideFrameTablesand aLocatedExprTree, enforcing the suppliedlimits. - encode_
frame - Encodes a bare
Exprinto aBinaryFrame, without source origins. - encode_
located_ frame - Encodes a
LocatedExprinto aBinaryFrame. - encode_
located_ tree_ frame - Encodes a
LocatedExprTreeinto aBinaryFrame.