pub trait CidCodec: Send + Sync {
// Required methods
fn caps(&self) -> CidCaps;
fn encode(&self, cid: ClientOrderId) -> Result<NativeCid, CidError>;
fn decode(&self, native: &NativeCid) -> Option<ClientOrderId>;
}Expand description
A pure, stateless mapping between ClientOrderId and the broker’s NativeCid.
Rules for an implementation:
encodeis a pure function: the same cid always gives the same string, with no state and no network.decode(encode(cid))givesSome(cid)for every cid (lossless). A broker whose field cannot keep a UUID needs another carrier field, not a lossy codec.decodegivesNonefor a string that this codec did not make (for example the cid of an order placed from the broker’s app).
§Examples
use brokapi_core::{CidCodec, ClientOrderId, HexUuidCodec};
let codec = HexUuidCodec;
let cid = ClientOrderId::new_v4();
let native = codec.encode(cid).unwrap();
assert_eq!(native.as_str().len(), 32);
assert_eq!(codec.decode(&native), Some(cid));Required Methods§
Sourcefn encode(&self, cid: ClientOrderId) -> Result<NativeCid, CidError>
fn encode(&self, cid: ClientOrderId) -> Result<NativeCid, CidError>
The broker form of cid.
§Errors
CidError::TooLong or CidError::InvalidChar when the result does not fit the field
described by caps. A correct codec never returns them for a UUID.
Sourcefn decode(&self, native: &NativeCid) -> Option<ClientOrderId>
fn decode(&self, native: &NativeCid) -> Option<ClientOrderId>
The cid behind a native string, or None if this codec did not make that string.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".