Skip to main content

TlsMap

Struct TlsMap 

pub struct TlsMap<K, V> { /* private fields */ }
Expand description

A sorted key-value map with deterministic TLS codec serialization.

Entries are maintained in sorted order by key, ensuring byte-identical serialization across all implementations. Keys must be unique.

Wire format: vlen(total_byte_length) || entry[0] || entry[1] || ... where each entry is K(tls) || V(tls) and vlen is the QUIC variable-length encoding (RFC 9000 §16).

§Examples

use xmtp_mls_tls_collections::tls_map::TlsMap;
use tls_codec::{Serialize, Deserialize};

let mut map = TlsMap::<u16, u16>::new();
map.insert(3, 30).unwrap();
map.insert(1, 10).unwrap();
map.insert(2, 20).unwrap();

// Serialization is deterministic regardless of insertion order
let bytes = map.tls_serialize_detached().unwrap();
let deserialized = TlsMap::<u16, u16>::tls_deserialize_exact(&bytes).unwrap();
assert_eq!(map, deserialized);

Implementations§

§

impl<K, V> TlsMap<K, V>
where K: Ord + Eq,

pub fn new() -> TlsMap<K, V>

Create an empty map.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let map = TlsMap::<u8, u16>::new();
assert!(map.is_empty());

pub fn from_pairs(iter: impl IntoIterator<Item = (K, V)>) -> TlsMap<K, V>

Create a map from an iterator of key-value pairs.

Entries are sorted by key. If duplicate keys exist, the last value wins.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let map = TlsMap::from_pairs([(2, "b"), (1, "a"), (2, "c")]);
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), Some(&"c")); // last value wins
assert_eq!(map.len(), 2);

pub fn insert(&mut self, key: K, value: V) -> Result<(), TlsMapError>

Insert a key-value pair. Returns an error if the key already exists.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let mut map = TlsMap::<u8, u8>::new();
assert!(map.insert(1, 10).is_ok());
assert!(map.insert(1, 20).is_err()); // duplicate key

pub fn update(&mut self, key: K, value: V) -> Result<(), TlsMapError>

Update an existing key’s value. Returns an error if the key doesn’t exist.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let mut map = TlsMap::<u8, u8>::new();
map.insert(1, 10).unwrap();
map.update(1, 20).unwrap();
assert_eq!(map.get(&1), Some(&20));
assert!(map.update(99, 0).is_err()); // key not found

pub fn set(&mut self, key: K, value: V)

Insert or update a key-value pair. Always succeeds.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let mut map = TlsMap::<u8, u8>::new();
map.set(1, 10);
map.set(1, 20); // overwrites
assert_eq!(map.get(&1), Some(&20));
assert_eq!(map.len(), 1);

pub fn remove(&mut self, key: &K) -> Result<V, TlsMapError>

Remove a key. Returns the removed value, or an error if the key doesn’t exist.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let mut map = TlsMap::<u8, u8>::new();
map.insert(1, 10).unwrap();
assert_eq!(map.remove(&1).unwrap(), 10);
assert!(map.remove(&1).is_err()); // already removed

pub fn get(&self, key: &K) -> Option<&V>

Get a value by key. Returns None if the key is not present.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let mut map = TlsMap::<u8, u8>::new();
map.insert(1, 10).unwrap();
assert_eq!(map.get(&1), Some(&10));
assert_eq!(map.get(&2), None);

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Get a mutable reference to a value by key. Returns None if the key is not present.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let mut map = TlsMap::<u8, u8>::new();
map.insert(1, 10).unwrap();
*map.get_mut(&1).unwrap() = 20;
assert_eq!(map.get(&1), Some(&20));

pub fn contains_key(&self, key: &K) -> bool

Check if a key exists in the map.

pub fn len(&self) -> usize

Returns the number of entries in the map.

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Iterate over key-value pairs in key-sorted order.

use xmtp_mls_tls_collections::tls_map::TlsMap;

let map = TlsMap::from_pairs([(2, "b"), (1, "a")]);
let pairs: Vec<_> = map.iter().collect();
assert_eq!(pairs, vec![(&1, &"a"), (&2, &"b")]);

pub fn keys(&self) -> impl Iterator<Item = &K>

Iterate over keys in sorted order.

pub fn values(&self) -> impl Iterator<Item = &V>

Iterate over values in key-sorted order.

§

impl<K, V> TlsMap<K, V>
where K: Ord + Eq,

pub fn apply_delta( &mut self, delta: TlsMapDelta<K, V>, ) -> Result<(), TlsMapError>

Apply a delta atomically. If any mutation fails, the map is unchanged.

Mutations apply in order, each to the result of the ones before it, and the first one that fails decides the error: an insert of a present key is TlsMapError::KeyExists, and an update or delete of an absent key is TlsMapError::KeyNotFound. The changed keys collect in an overlay that is merged into the entries once, after every mutation succeeds.

use xmtp_mls_tls_collections::tls_map::{TlsMap, TlsMapDelta};

let mut map = TlsMap::<u8, u8>::new();
map.insert(1, 10).unwrap();

let delta = TlsMapDelta::new()
    .insert(2, 20)
    .update(1, 15)
    .delete(1);

map.apply_delta(delta).unwrap();
assert_eq!(map.get(&2), Some(&20));
assert!(!map.contains_key(&1));

Trait Implementations§

§

impl<K, V> Clone for TlsMap<K, V>
where K: Clone, V: Clone,

§

fn clone(&self) -> TlsMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<K, V> Debug for TlsMap<K, V>
where K: Debug, V: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<K, V> Default for TlsMap<K, V>

§

fn default() -> TlsMap<K, V>

Returns the “default value” for a type. Read more
§

impl<K, V> Deserialize for TlsMap<K, V>
where K: Deserialize + Size + Ord + Eq, V: Deserialize + Size,

§

fn tls_deserialize<R>(bytes: &mut R) -> Result<TlsMap<K, V>, Error>
where R: Read, TlsMap<K, V>: Sized,

This function deserializes the bytes from the provided a std::io::Read and returns the populated struct. Read more
§

fn tls_deserialize_exact(bytes: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

This function deserializes the provided bytes and returns the populated struct. All bytes must be consumed. Read more
§

impl<K, V> FromIterator<(K, V)> for TlsMap<K, V>
where K: Ord + Eq,

§

fn from_iter<T>(iter: T) -> TlsMap<K, V>
where T: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
§

impl<K, V> IntoIterator for TlsMap<K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = Map<IntoIter<TlsMapEntry<K, V>>, fn(TlsMapEntry<K, V>) -> (K, V)>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> <TlsMap<K, V> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
§

impl<K, V> PartialEq for TlsMap<K, V>
where K: PartialEq, V: PartialEq,

§

fn eq(&self, other: &TlsMap<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<K, V> Serialize for TlsMap<K, V>
where K: Serialize + Size + Debug, V: Serialize + Size + Debug,

§

fn tls_serialize<W>(&self, writer: &mut W) -> Result<usize, Error>
where W: Write,

Serialize self and write it to the writer. The function returns the number of bytes written to writer.
§

fn tls_serialize_detached(&self) -> Result<Vec<u8>, Error>

Serialize self and return it as a byte vector.
§

impl<K, V> Size for TlsMap<K, V>
where K: Size, V: Size,

§

impl<K, V> Eq for TlsMap<K, V>
where K: Eq, V: Eq,

§

impl<K, V> StructuralPartialEq for TlsMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for TlsMap<K, V>

§

impl<K, V> RefUnwindSafe for TlsMap<K, V>

§

impl<K, V> Send for TlsMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for TlsMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for TlsMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnsafeUnpin for TlsMap<K, V>

§

impl<K, V> UnwindSafe for TlsMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

§

impl<T> AggregateExpressionMethods for T

§

fn aggregate_distinct(self) -> Self::Output
where Self: DistinctDsl,

DISTINCT modifier for aggregate functions Read more
§

fn aggregate_all(self) -> Self::Output
where Self: AllDsl,

ALL modifier for aggregate functions Read more
§

fn aggregate_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add an aggregate function filter Read more
§

fn aggregate_order<O>(self, o: O) -> Self::Output
where Self: OrderAggregateDsl<O>,

Add an aggregate function order Read more
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

§

const WITNESS: W = W::MAKE

A constant of the type witness
§

impl<T> Identity for T
where T: ?Sized,

§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<T> IntoSql for T

§

fn into_sql<T>(self) -> Self::Expression
where Self: Sized + AsExpression<T>, T: SqlType + TypedExpressionType,

Convert self to an expression for Diesel’s query builder. Read more
§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

Convert &self to an expression for Diesel’s query builder. Read more
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WindowExpressionMethods for T

§

fn over(self) -> Self::Output
where Self: OverDsl,

Turn a function call into a window function call Read more
§

fn window_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add a filter to the current window function Read more
§

fn partition_by<E>(self, expr: E) -> Self::Output
where Self: PartitionByDsl<E>,

Add a partition clause to the current window function Read more
§

fn window_order<E>(self, expr: E) -> Self::Output
where Self: OrderWindowDsl<E>,

Add a order clause to the current window function Read more
§

fn frame_by<E>(self, expr: E) -> Self::Output
where Self: FrameDsl<E>,

Add a frame clause to the current window function Read more
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

§

impl<T> MaybeSync for T
where T: Sync + ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,