Skip to main content

Decoder

Trait Decoder 

Source
pub trait Decoder<'id>: Sized {
    // Required method
    fn decode(
        term: AnyTerm<'id>,
        env: impl Env<'id>,
    ) -> Result<Self, CodecError>;
}
Expand description

Extract a value from an Erlang term of brand 'id.

Implemented by otter term types. Takes the raw AnyTerm plus an env (the term carries only its brand, not its env), so each impl pays exactly the type check it needs. Returns Err(CodecError) if the term is not the expected type or the value does not fit.

Required Methods§

Source

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>, G: Decoder<'id>, H: Decoder<'id>, I: Decoder<'id>, J: Decoder<'id>, K: Decoder<'id>, L: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F, G, H, I, J, K, L)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>, G: Decoder<'id>, H: Decoder<'id>, I: Decoder<'id>, J: Decoder<'id>, K: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F, G, H, I, J, K)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>, G: Decoder<'id>, H: Decoder<'id>, I: Decoder<'id>, J: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F, G, H, I, J)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>, G: Decoder<'id>, H: Decoder<'id>, I: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F, G, H, I)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>, G: Decoder<'id>, H: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F, G, H)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>, G: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F, G)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>, F: Decoder<'id>> Decoder<'id> for (A, B, C, D, E, F)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>, E: Decoder<'id>> Decoder<'id> for (A, B, C, D, E)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>, D: Decoder<'id>> Decoder<'id> for (A, B, C, D)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>, C: Decoder<'id>> Decoder<'id> for (A, B, C)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>, B: Decoder<'id>> Decoder<'id> for (A, B)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, A: Decoder<'id>> Decoder<'id> for (A,)

Requires an Erlang tuple of exactly N elements (WrongArity otherwise), then decodes each element in turn; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, K, V, S> Decoder<'id> for HashMap<K, V, S>
where K: Decoder<'id> + Eq + Hash, V: Decoder<'id>, S: BuildHasher + Default,

Requires an Erlang map, decoding each pair into the HashMap. Generic over the hasher S. A key/value’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id, T: Decoder<'id>> Decoder<'id> for Vec<T>

Requires a proper list, decoding each element into the Vec. An improper tail is WrongType; an element’s own error propagates.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for String

Accepts a binary (read as UTF-8) or a charlist (list of codepoints). Invalid UTF-8 / a non-string list is NotUtf8; a term that is neither binary nor list is WrongType.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for bool

Accepts exactly the atoms true/false; every other term — including any other atom — is WrongType.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for f32

Reads as f64 and narrows; a magnitude beyond f32::MAX would round to infinity, so it is rejected with FloatRange.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for f64

Reads a float term (enif_get_double); an Erlang float is always finite, so this never fails on range. WrongType if not a float.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for i8

Reads the integer as i64, then narrows with a range check — IntegerOverflow if it does not fit.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for i16

Reads the integer as i64, then narrows with a range check — IntegerOverflow if it does not fit.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for i32

Reads the integer as i64, then narrows with a range check — IntegerOverflow if it does not fit.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for i64

Reads via enif_get_int64; the only failure is a bignum too wide for the 64-bit read (IntegerOverflow).

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for isize

Reads as i64 and range-checks into isize (IntegerOverflow if it does not fit).

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for u8

Reads the integer as u64, then narrows with a range check — IntegerOverflow on overflow or a negative value.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for u16

Reads the integer as u64, then narrows with a range check — IntegerOverflow on overflow or a negative value.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for u32

Reads the integer as u64, then narrows with a range check — IntegerOverflow on overflow or a negative value.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for u64

Reads via enif_get_uint64; fails with IntegerOverflow on a negative value or a bignum too wide for the 64-bit read.

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Source§

impl<'id> Decoder<'id> for usize

Reads as u64 and range-checks into usize (IntegerOverflow on a negative value or overflow).

Source§

fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError>

Implementors§

Source§

impl<'id, T: Resource> Decoder<'id> for ResourceArc<T>

Source§

impl<'id> Decoder<'id> for AnyTerm<'id>

The identity decode — any term decodes to itself.

Source§

impl<'id> Decoder<'id> for Atom

Accepts an atom (enif_is_atom); WrongType otherwise.

Source§

impl<'id> Decoder<'id> for BigInt

Available on crate feature bigint only.

Reads any integer term, however wide, via Integer::to_bigint. A non-integer term is WrongType.

Source§

impl<'id> Decoder<'id> for Binary<'id>

Accepts a byte-aligned binary (enif_is_binary); a sub-byte bitstring is rejected as WrongType — decode to Bitstring for those.

Source§

impl<'id> Decoder<'id> for Bitstring<'id>

Accepts any bitstring (enif_term_type == Bitstring) — both byte-aligned binaries and sub-byte bitstrings. Refine to Binary for the byte-aligned case. WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Float<'id>

Accepts a float term (enif_term_type == Float); WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Fun<'id>

Accepts a fun (enif_is_fun); WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Integer<'id>

Accepts an integer term (enif_term_type == Integer); rejects anything else as WrongType.

Source§

impl<'id> Decoder<'id> for List<'id>

Accepts a list — including the empty list, and improper lists (enif_is_list). WrongType otherwise.

Source§

impl<'id> Decoder<'id> for LocalPid

Accepts a node-local pid (enif_get_local_pid); an external (remote-node) pid is rejected as WrongType.

Source§

impl<'id> Decoder<'id> for LocalPort

Accepts a node-local port; an external port is rejected as WrongType.

Source§

impl<'id> Decoder<'id> for Map<'id>

Accepts a map (enif_is_map); WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Pid<'id>

Accepts a pid of any locality (enif_is_pid); to require a node-local pid, decode to LocalPid. WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Port<'id>

Accepts a port of any locality (enif_is_port); to require a node-local port, decode to LocalPort. WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Reference<'id>

Accepts a reference (enif_is_ref); WrongType otherwise.

Source§

impl<'id> Decoder<'id> for Tuple<'id>

Accepts a tuple of any arity (enif_is_tuple); WrongType otherwise.

Source§

impl<'id> Decoder<'id> for TypedTerm<'id>

Resolves the term to its TypedTerm variant (one enif_term_type call). A type code this otter build does not recognize — a term type from a newer OTP — is UnknownTermType.