pub trait WitScalarCodec {
// Required methods
fn wit_name(&self) -> &str;
fn wit_interface(&self) -> Option<&str>;
fn encode(&self, token: &str) -> Result<Vec<Felt>, TypedError>;
fn decode(&self, felts: &[Felt]) -> Result<String, TypedError>;
}Expand description
How to encode and print one WIT type.
A type name in a signature is a full WIT path, like miden:base/core-types@1.0.0/word.
We match a codec on two parts: the plain name from Self::wit_name and the interface from
Self::wit_interface. If both match, the codec does the work: one token in, the felts of the
type out, and the other way around for results.
The type says how many felts a value takes, not the codec. So a codec cannot disagree with the signature.
TypedProcInfo adds WordCodec and FeltCodec for you. Types
with rules from outside this crate need a codec from the user. See
TypedProcInfo::with_scalar_codec.
Required Methods§
Sourcefn wit_interface(&self) -> Option<&str>
fn wit_interface(&self) -> Option<&str>
The WIT interface this codec belongs to, without the version. For example
MIDEN_CORE_TYPES for the core types, or miden:shapes/points for your own types.
This stops a codec from taking a type that only has the same plain name. Some other
miden:shapes/points/word is not the core word. If the two have the same width, we would
read a hex token and get a wrong value, and nobody would see the problem.
None means we match on the plain name only, from any interface. This also covers a plain
type name that is not WIT, because it has no interface.
There is no default on purpose. Both answers are wrong in some case, and neither one gives an error. With a default interface, a codec for your own type would never match, and we would read its arguments field by field. With no default, a codec could take a type that is not its own. So you must choose.
Sourcefn encode(&self, token: &str) -> Result<Vec<Felt>, TypedError>
fn encode(&self, token: &str) -> Result<Vec<Felt>, TypedError>
Turns one token into the felts of this type.
Sourcefn decode(&self, felts: &[Felt]) -> Result<String, TypedError>
fn decode(&self, felts: &[Felt]) -> Result<String, TypedError>
Turns the felts of one value into text, like word(0x..). Returns an error if the felts
are not a valid value of this type.
The error goes to the caller. We do not fall back to the field by field way. If we printed the fields of a value the codec just called bad, a bad value would look good. And if we dropped the error, the caller could not tell a bad result from a procedure that returns nothing.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".