Skip to main content

Encoder

Trait Encoder 

Source
pub trait Encoder<'id> {
    // Required method
    fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>;
}
Expand description

Convert a value into an Erlang term of brand 'id.

Fallible, mirroring Decoder: a value outside the Erlang term domain (e.g. a non-finite f64) returns Err(CodecError). The #[otter::nif] return path turns an Err into a badret exception — symmetric to the badarg a failed Decoder raises on the way in. Impls that cannot fail — every otter term type encodes by wrapping its word for free — return Ok.

A same-brand term encodes for free; cross-env terms are not encoded — copy them first with Term::copy_to.

Required Methods§

Source

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, 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: Encoder<'id>, B: Encoder<'id>, C: Encoder<'id>, D: Encoder<'id>, E: Encoder<'id>, F: Encoder<'id>, G: Encoder<'id>, H: Encoder<'id>, I: Encoder<'id>, J: Encoder<'id>, K: Encoder<'id>, L: Encoder<'id>> Encoder<'id> for (A, B, C, D, E, F, G, H, I, J, K, L)

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Builds an Erlang N-tuple from the encoded elements (enif_make_tuple_from_array, no heap allocation). Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id, K: Encoder<'id>, V: Encoder<'id>, S> Encoder<'id> for HashMap<K, V, S>

Builds an Erlang map from the encoded keys and values in one enif_make_map_from_arrays, falling back to incremental insertion only if the encoded keys collide (a non-injective key Encoder). Fails if any key/value fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id, T: Encoder<'id>> Encoder<'id> for Result<T, Raised<'id>>

Encode a Result<T, Raised> in return position only.

Ok(v) encodes v. Err(raised) carries proof that an exception is already pending on the env, so the non-value marker is returned and the BEAM raises the pending exception on NIF return. Never encode an Err mid-term (inside a tuple/list/map) — that diverts the marker into a value position. Always propagate a Result<T, Raised> with ? or return.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

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

Encodes as a proper list, via the [T] impl.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id, T: Encoder<'id>> Encoder<'id> for [T]

Builds a proper Erlang list from the element-wise encodings in one enif_make_list_from_array. Fails if any element fails to encode.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for String

Encodes as a binary, via str’s impl. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for bool

Yields the atom true or false. Infallible — both are always interned.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for f32

Widens to f64 and encodes, rejecting non-finite values with NotFinite.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for f64

Rejects a non-finite value (NaN/infinity) with NotFinite; otherwise enif_make_double.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for i8

Widens losslessly into the i64 of enif_make_int64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for i16

Widens losslessly into the i64 of enif_make_int64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for i32

Widens losslessly into the i64 of enif_make_int64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for i64

Maps straight onto enif_make_int64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for isize

64-bit on every otter target; casts to i64 and encodes. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for str

Encodes the UTF-8 bytes as a binary (enif_make_new_binary), the modern convention. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for u8

Widens losslessly into the u64 of enif_make_uint64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for u16

Widens losslessly into the u64 of enif_make_uint64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for u32

Widens losslessly into the u64 of enif_make_uint64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for u64

Maps straight onto enif_make_uint64. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Source§

impl<'id> Encoder<'id> for usize

64-bit on every otter target; casts to u64 and encodes. Infallible.

Source§

fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError>

Implementors§

Source§

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

Source§

impl<'id> Encoder<'id> for AnyTerm<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Atom

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for BigInt

Available on crate feature bigint only.

Encodes any magnitude via Integer::from_bigint (an i64/u64 fast path, else ETF). Infallible.

Source§

impl<'id> Encoder<'id> for Binary<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Bitstring<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Float<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Fun<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Integer<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for List<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for LocalPid

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for LocalPort

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Map<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Pid<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Port<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Reference<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for Tuple<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for TupleView<'id>

Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.

Source§

impl<'id> Encoder<'id> for TypedTerm<'id>

Encodes the already-resolved term by rewrapping its word. Never fails.