with_conversion vs with_domain_type
This is the only full comparison of with_conversion and with_domain_type.
An owned DTO (with_domain_objects) is a different switch:
Domain objects,
Domain DTOs,
Flyweight vs whole struct.
Do not call both for the same selector: a domain type already enables conversion.
A: with_conversion | B: with_domain_type | |
|---|---|---|
| Idea | Generic convert API; you plug in any app type | Always use this Rust path |
| build.rs | .with_conversion(named_type("Decimal")) | .with_domain_type(…, "rust_decimal::Decimal") |
| You write | TryFromSbe<Decimal> / TryToSbe<Decimal> for your type | Usually nothing for bool / rust_decimal / chrono |
| Decode | let p: Cents = dec.price_as()? | let p: rust_decimal::Decimal = dec.price()? |
| Encode | enc.price_from(¢s)? | enc.price(rust_decimal::Decimal::new(12345, 2))? |
| Raw wire | price_value() / price_wire(...) | same when conversion is active |
| Sample | exchange-example · demo_conversion_only | l3-book |
Which fields a selector reaches, and what shape you get
A selector matches by precedence (exact FieldPath, then SemanticType, then
an ExtensionAttr schema mark, then NamedType), so a blanket mapping plus
one per-field override behaves the way it reads, whatever order you register
them in. Paths are "Message.field", extended with group names inside a group
("Order.legs.price").
extension_attr("exponent") selects fields whose schema carries a namespaced
exponent attribute (any prefix); extension_attr_value("exponent", "price")
additionally requires that exact value. Marks select fields only — the Rust
type always comes from build.rs, never from the schema. ExtensionAttr
reaches message and group fields (nested group entries included); marks on
<data>, <group> elements and type-level declarations are visible to
hooks only.
named_type matches a composite, enum or set by name, or a primitive alias:
with <type name="Timestamp" primitiveType="uint64"/>,
named_type("Timestamp") selects every field declared type="Timestamp", in
messages and groups. It never matches a bare primitive (uint64).
A selector that cannot apply fails generation instead of being skipped:
| Config | GenerateError |
|---|---|
field_path / semantic_type / extension_attr that matches no field | InvalidConversion: “matches no field in the schema” |
named_type naming no composite, enum or set, and no alias a field uses | InvalidConversion: “no matching type found in schema” |
with_domain_type / with_manual_domain_type twice on one selector, different types | InvalidConversion: “mapped to both A and B” |
| a domain-type selector whose every field a higher-precedence selector takes | InvalidConversion: “every field it matches takes a higher-precedence selector” |
| two extension domain selectors tie on one field with different types | InvalidConversion: “ambiguous extension selectors for field …” |
with_null_as_option on anything but named_type(<enum>) | InvalidConfiguration |
When the schema has names close to an unknown one, the reason ends
(did you mean …?) with up to three of them.
generate_multi resolves each selector against the union of its schemas. A
later with_enum_mapping / with_composite_mapping for the same type still
replaces an earlier one.
What the accessor gives you depends on the field’s shape, because it mirrors the raw accessor it delegates to:
| Field | with_conversion | with_domain_type |
|---|---|---|
Required, sinceVersion=0 | price_as::<T>() -> Result<T, _> | price() -> Result<T, _> |
sinceVersion > 0 | Result<Option<T>, _> | Result<Option<T>, _> |
presence="optional" scalar | Result<Option<T>, _> | Result<Option<T>, _> |
The Option layer means “no value on the wire”: absent at this version, or
the schema null value. It is never used to report a bad value; an invalid wire
value is the Err.
Composites, enums, and sets all take domain types. Fixed arrays and optional
scalars keep their wire type in a domain DTO even when a selector matches
them. The flyweight still gets the converted accessor, but the owned struct
stores [u8; N] / Option<i64>.
field_path(p) · FieldPathp equals the path:
"Message.field", or "Order.legs.price" in a groupsemantic_type(s) · SemanticTypes equals the field’s
semanticTypeextension_attr(a) · ExtensionAttrthe field carries schema extension attribute a, any namespace;
extension_attr_value(a, v) also needs value v. Message and group fields only.named_type(n) · NamedTypen equals a composite, enum or set name, or a primitive field’s
<type> alias. named_type("uint64") matches nothing: a bare built-in has no name.0 · FieldPath
1 · SemanticType
2 · ExtensionAttr
3 · NamedType
whatever order the selectors were registered in
field_path, semantic_type or extension_attr matching no field: “matches no field in the schema”named_type naming nothing: “no matching type found in schema”A and B”extension_attr domain types tying on one field: “ambiguous extension selectors for field F”never
Option: Result<T, _> (zero-filled when the field predates the version)Result<Option<T>, _> if sinceVersion > 0 or presence="optional", else Result<T, _>Result<Option<T>, _> if sinceVersion > 0 or with_null_as_option maps it, else Result<T, _>Result<Option<T>, _> if sinceVersion > 0, else Result<T, _>Quote.price: its type is the Decimal composite, and its path is "Quote.price".Quote.price takes the field_path mapping. Other Decimal fields keep the named_type one.Quote.price), a domain-type named_type has none left: “every field it matches takes a higher-precedence selector”.Quote.price is a composite: the domain getter price() returns Result<T, _> (T from field_path), or Result<Option<T>, _> if its sinceVersion > 0.GenerateError::InvalidConversionf_as::<T>() or domain f() returnsNone means no value on the wire; an invalid wire value is the Err.named_type("Decimal") and field_path("Quote.price") as domain types, in a schema with more Decimal fieldsOption A: you choose the app type (Cents)
#![allow(unused)]
fn main() {
use ergo_sbe::{ConversionSelector, GenerationConfig};
// A — generic converter: one wire type, many app types
let _cfg =
GenerationConfig::new("msgs").with_conversion(ConversionSelector::named_type("Decimal"));
}
(From book/examples/conversion-config.rs, a self-contained program that compiles against ergo-sbe.)
#![allow(unused)]
fn main() {
use rust_decimal::Decimal as Rd;
// App adapter: wire Decimal ↔ rust_decimal::Decimal
struct FixedPrice { mantissa: i64, exponent: i8 }
impl TryFromSbe<Decimal> for FixedPrice {
type Error = &'static str;
fn try_from_sbe(wire: Decimal) -> Result<Self, Self::Error> {
Ok(FixedPrice {
mantissa: wire.mantissa(),
exponent: wire.exponent(),
})
}
}
impl TryToSbe<Decimal> for FixedPrice {
type Error = &'static str;
fn try_to_sbe(&self) -> Result<Decimal, Self::Error> {
Ok(Decimal::new(self.mantissa, self.exponent))
}
}
}
(From book/examples/conversion-app-code.rs: the app adapter pattern, compiled against tour_codec.)
// Encode using the generic conversion API:
let mut buf = [0u8; QuoteEncoder::compute_length_with_header()];
let price = Rd::new(12345, 2); // 123.45
let mut enc = QuoteEncoder::try_wrap_and_apply_header(&mut buf, 0)?;
enc.price_from(&price)?;
enc.size_from(&Rd::new(10, 0))?;
let len = QuoteEncoder::compute_length_with_header();
// Decode — generic `_as::<T>()` picks your adapter:
let dec = QuoteDecoder::try_from(&buf[..len])?;
let p: Rd = dec.price_as()?;
assert_eq!(p, Rd::new(12345, 2));
// Same buffer, different app type — only possible with with_conversion:
let fixed: FixedPrice = dec.price_as()?;
assert_eq!(fixed.mantissa, 12345);
assert_eq!(fixed.exponent, -2);
(Same file: generic _from/_as encode/decode with with_conversion.)
Option B: one fixed app type
#![allow(unused)]
fn main() {
use ergo_sbe::{ConversionSelector, GenerationConfig};
// B — concrete mapping: one Rust type per wire type (already enables conversion)
let _cfg = GenerationConfig::new("msgs").with_domain_type(
ConversionSelector::named_type("Decimal"),
"rust_decimal::Decimal",
);
}
(Same source file, book/examples/conversion-config.rs.)
Under with_domain_type the accessors are concrete: price(value)? writes and
price()? reads, with no price_from or price_as for that field.
Both styles on different fields:
pub fn demo_conversion_only() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let mut buf = [0u8; QuoteEncoder::compute_length_with_header()];
let price = Rd::new(12345, 2); // 123.45
let size = Rd::new(10, 0);
let mut enc = QuoteEncoder::try_wrap_and_apply_header(&mut buf, 0)?;
enc.price_from(&price)?;
enc.size_from(&size)?;
let len = QuoteEncoder::compute_length_with_header();
let dec = QuoteDecoder::try_from(&buf[..len])?;
let wire = dec.price_value();
assert_eq!(wire.mantissa(), 12345);
assert_eq!(wire.exponent(), -2);
let price2: Rd = dec.price_as()?;
let size2: Rd = dec.size_as()?;
assert_eq!(price2, price);
assert_eq!(size2, size);
// Same buffer, different app type, only possible with with_conversion.
let fixed: FixedPrice = dec.price_as()?;
assert_eq!(
fixed,
FixedPrice {
mantissa: 12345,
exponent: -2
}
);
let dto = QuoteDomain::try_from_decoder(dec)?;
assert_eq!(dto.price.mantissa(), 12345);
let mut re = [0u8; QuoteEncoder::compute_length_with_header()];
let n = dto.encode(&mut re)?;
assert_eq!(&re[..n], &buf[..len]);
Ok(buf[..len].to_vec())
}
(This code comes from the sbe-feature-tour sample crate.)
Option B, manual impl: concrete signatures, your own conversion logic
with_domain_type(selector, path) is the common case: ergo-sbe also generates
the TryFromSbe/TryToSbe impl for bool / rust_decimal::Decimal /
chrono::DateTime<Utc>. If you need different conversion behaviour for one of
those exact three types (a custom rounding rule, stricter validation,
different null handling), call additive with_manual_domain_type
instead: same generated price(...)? / price()? signatures, but you
write the impl:
let (_schema, src) = generate_domain_with(&path, "manual_impl_dt", |c| {
c.with_manual_domain_type(
ergo_sbe::ConversionSelector::named_type("Decimal"),
"rust_decimal::Decimal",
)
});
use rust_decimal::Decimal;
// Caller-supplied impl: deliberately scales mantissa by 10 on the way
// in/out, so a value round-trips only through this impl, which
// proves ergo-sbe did not generate its own.
impl TryFromSbe<self::Decimal> for rust_decimal::Decimal {
type Error = &'static str;
fn try_from_sbe(wire: self::Decimal) -> Result<Self, Self::Error> {
Ok(rust_decimal::Decimal::new(wire.mantissa() / 10, (-wire.exponent()) as u32))
}
}
impl TryToSbe<self::Decimal> for rust_decimal::Decimal {
type Error = &'static str;
fn try_to_sbe(&self) -> Result<self::Decimal, Self::Error> {
Ok(self::Decimal::new(self.mantissa() as i64 * 10, -(self.scale() as i8)))
}
}
let mut buf = [0u8; 256];
let mut enc = OrderEncoder::wrap_and_apply_header(&mut buf, 0)
.fixed(&OrderFixedFields { price: self::Decimal::new(0, 0), size: self::Decimal::new(0, 0) });
enc.price(Decimal::new(12345, 2))?;
enc.size(Decimal::new(100, 0))?;
let encoded = enc.as_bytes_with_header().to_vec();
let dec = OrderDecoder::try_decode(&encoded, 0)?;
assert_eq!(dec.price()?, Decimal::new(12345, 2));
assert_eq!(dec.size()?, Decimal::new(100, 0));
(From domain_type_manual_impl_uses_callers_own_impl in
sbe/tests/baseline_test.rs, a real generated-and-compiled test. Any
rust_type string that isn’t one of the three built-ins never gets an
auto-generated impl regardless of DomainImpl; the setting only matters for
opting those three in or out.)
If you forget the impl, two things soften it. First, the compile error names the missing impl directly instead of the default trait-bound message:
error[E0277]: `rust_decimal::Decimal` has no `TryFromSbe<Decimal>` impl
|
| missing `impl TryFromSbe<Decimal> for rust_decimal::Decimal`
|
= note: if this field uses DomainImpl::Manual, the generated domain
accessor's doc comment has a ready-to-paste starting point
Second, for the three built-ins, that pointer is real: the generated
price method’s own doc comment (visible on hover, or in cargo doc)
carries the exact impl DomainImpl::Generated would have written; copy it
out and adjust. sbe/tests/baseline_test.rs’s
domain_type_manual_impl_doc_comment_has_generated_snippet asserts this
snippet is present in the generated source.
What your domain type must implement
Only Debug. The generated Display/Debug bodies format domain values with
{:?}, so a plain #[derive(Debug)] struct works; Display is never
required. Note that TryFromSbe::Error / TryToSbe::Error is a separate
associated type and does need Debug + Display; &'static str satisfies it.
Option C: generated mappings for enums and composites
When the application type mirrors the SBE type (an enum with the same
variants, a struct with the same members), with_enum_mapping and
with_composite_mapping generate the TryFromSbe / TryToSbe impls
(exhaustive, with renames) and register the domain type in one call. The
accessors look exactly like Option B’s. See
App Types on Composites.