Expand description
Inline types, schemas, and conversion traits.
Inline<S> (32-byte stored payload), Encoded<V> (the
Inline-or-Blob sum that entity!{} builds), and the conversion
traits between them. For a deeper look at portability goals,
common formats, and schema design, refer to the “Portability &
Common Formats” chapter in the project book.
§Example
use triblespace_core::inline::{Inline, InlineEncoding, IntoInline, TryFromInline};
use triblespace_core::metadata::{self, MetaDescribe};
use triblespace_core::trible::Fragment;
use triblespace_core::id::{ExclusiveId, Id};
use triblespace_core::macros::{id_hex, entity};
use std::convert::{TryInto, Infallible};
// Define a new schema type.
// We're going to define an unsigned integer type that is stored as a little-endian 32-byte array.
// Note that makes our example easier, as we don't have to worry about sign-extension or padding bytes.
pub struct MyNumber;
// The schema's identity hex lives inline in its describe body — that's
// the only place it appears; callers reach the id via MyNumber::id().
// `entity!{ &id @ ... }` returns a Fragment already rooted at `id` with
// the listed facts; auto-puts any blob-source values into its local
// store. The `metadata::tag` annotation lets schema registries discover
// this entity as an inline encoding.
impl MetaDescribe for MyNumber {
fn describe() -> Fragment {
let id: Id = id_hex!("345EAC0C5B5D7D034C87777280B88AE2");
entity! { ExclusiveId::force_ref(&id) @
metadata::name: "my_number",
metadata::tag: metadata::KIND_INLINE_ENCODING,
}
}
}
impl InlineEncoding for MyNumber {
type ValidationError = ();
type Encoding = Self;
// Every bit pattern is valid for this schema.
}
// Implement conversion functions for the schema type.
// Use `Error = Infallible` when the conversion cannot fail.
impl TryFromInline<'_, MyNumber> for u32 {
type Error = Infallible;
fn try_from_inline(v: &Inline<MyNumber>) -> Result<Self, Infallible> {
Ok(u32::from_le_bytes(v.raw[0..4].try_into().unwrap()))
}
}
impl triblespace_core::inline::IntoEncoded<MyNumber> for u32 {
type Output = Inline<MyNumber>;
fn into_encoded(self) -> Inline<MyNumber> {
// Convert the Rust type to the schema type, i.e. a 32-byte array.
let mut bytes = [0; 32];
bytes[0..4].copy_from_slice(&self.to_le_bytes());
Inline::new(bytes)
}
}
// Use the schema type to store and retrieve a Rust type.
let value: Inline<MyNumber> = MyNumber::inline_from(42u32);
let i: u32 = value.from_inline();
assert_eq!(i, 42);
// You can also implement conversion functions for other Rust types.
impl TryFromInline<'_, MyNumber> for u64 {
type Error = Infallible;
fn try_from_inline(v: &Inline<MyNumber>) -> Result<Self, Infallible> {
Ok(u64::from_le_bytes(v.raw[0..8].try_into().unwrap()))
}
}
impl triblespace_core::inline::IntoEncoded<MyNumber> for u64 {
type Output = Inline<MyNumber>;
fn into_encoded(self) -> Inline<MyNumber> {
let mut bytes = [0; 32];
bytes[0..8].copy_from_slice(&self.to_le_bytes());
Inline::new(bytes)
}
}
let value: Inline<MyNumber> = MyNumber::inline_from(42u64);
let i: u64 = value.from_inline();
assert_eq!(i, 42);
// And use a value round-trip to convert between Rust types.
let value: Inline<MyNumber> = MyNumber::inline_from(42u32);
let i: u64 = value.from_inline();
assert_eq!(i, 42);Modules§
- encodings
- Built-in inline encoding types and their conversion implementations. This is a collection of Rust types that can be (de)serialized as crate::prelude::Inlines.
Structs§
- Inline
- A value is a 32-byte array that can be (de)serialized as a Rust type. The schema type parameter is an abstract type that represents the meaning and valid bit patterns of the bytes.
Enums§
- Encoded
- The two-shape sum an attribute’s value can take when an
entity!{}field is encoded: either a 32-byteInline<V>payload that lives directly in the trible, or a [Blob] holding the heavy content with a derivable handle.
Constants§
- INLINE_
LEN - The length of a value in bytes.
Traits§
- Encodes
- User-implemented schema-side encoding trait, in the
Fromdirection: the schema is the impl target, the source is the trait parameter. - Inline
Encoding - A trait that represents an abstract schema type that can be (de)serialized as a Inline.
- Into
Encoded - Source-side ergonomic counterpart of
Encodes, in theIntodirection: methods like42u32.to_inline()resolve here. - Into
Inline - Shorthand bound for
IntoEncoded<S, Output = Inline<S>>— “this source produces a directly-encodedInline<S>, no side-blob.” - ToEncoded
- Lift an
IntoEncoded::Outputinto theEncodedsum theentity!{}macro folds into a Fragment. - TryFrom
Inline - A trait for converting a Inline with a specific schema type to a Rust type. This trait is implemented on the concrete Rust type.
- TryTo
Inline - Fallible variant of value conversion —
T → Result<Inline<S>, Error>.
Type Aliases§
- RawInline
- A raw value is simply a 32-byte array.