Skip to main content

OrderedMap

Struct OrderedMap 

Source
pub struct OrderedMap { /* private fields */ }
Expand description

remove shifts later entries down. Lookups are linear; that is the intended trade for small maps and stable ordering.

PartialEq is hand-written, not derived (issue #909, ruled 2026-07-18 — docs/decision-log.md “Map/record equality is insertion-order-insensitive”): equality is content-based, comparing key→value pairs regardless of insertion order — #{a:1,b:2} == #{b:2,a:1} is true. Only equality ignores order; iter/keys/values and every codec still walk entries in insertion order, unchanged. The derived PartialEq this replaces compared entries as a Vec, which is order-sensitive — the bug. Value::Map’s Arc::ptr_eq fast path (same snapshot → instant true) lives one level up in impl PartialEq for Value; this impl is the structural fallback it calls into.

Implementations§

Source§

impl OrderedMap

Source

pub fn new() -> Self

Create an empty map.

Source

pub fn with_capacity(n: usize) -> Self

Create an empty map with capacity for n entries.

Source

pub fn len(&self) -> usize

Number of entries.

Source

pub fn is_empty(&self) -> bool

Whether the map has no entries.

Source

pub fn get(&self, key: &MapKey) -> Option<&Value>

Borrow the value for key, or None if absent.

Source

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

Whether key is present.

Source

pub fn get_mut(&mut self, key: &MapKey) -> Option<&mut Value>

Mutably borrow the value for key, or None if absent. The intermediate-segment leg of the T1e projection RMW spine (docs/t1e-spec.md §3): recursing a make_mut chain through a map needs a mutable handle to an existing entry without touching insertion order, which insert alone (add-or-replace) can’t provide.

Source

pub fn insert(&mut self, key: MapKey, value: Value) -> Option<Value>

Insert value under key, returning the previous value if the key was already present.

An existing key keeps its insertion position (only its value changes); a new key is appended, so first-insertion order is preserved.

Source

pub fn remove(&mut self, key: &MapKey) -> Option<Value>

Remove key, returning its value if it was present. Later entries shift down, so insertion order among the survivors is preserved.

Source

pub fn iter(&self) -> impl Iterator<Item = (&MapKey, &Value)>

Iterate (key, value) pairs in insertion order.

Source

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

Iterate keys in insertion order.

Source

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

Iterate values in insertion order.

Trait Implementations§

Source§

impl Clone for OrderedMap

Source§

fn clone(&self) -> OrderedMap

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
Source§

impl Debug for OrderedMap

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for OrderedMap

Source§

fn default() -> OrderedMap

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

impl<'de> Deserialize<'de> for OrderedMap

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Hand-written, not derived (issue #985, follow-up to #909): the derived impl would deserialize entries verbatim as a Vec<(MapKey, Value)>, letting a crafted or corrupt payload carry a duplicate key and construct a map that violates the content-based Eq invariant above — Eq assumes each key appears at most once. This decodes into the same shape the derive would have produced, then walks the entries through the same duplicate-key check the .inkb/.inkt/transcript decoders use (rejecting rather than silently keeping the last occurrence — a legitimate encoder never emits a repeat, since insert de-duplicates on the write side, so a repeat is corrupt input, never a panic).

Source§

impl FromIterator<(MapKey, Value)> for OrderedMap

Source§

fn from_iter<I: IntoIterator<Item = (MapKey, Value)>>(iter: I) -> Self

Collect entries in order. If a key repeats, the last value wins while the key keeps its first-insertion position — matching insert.

Source§

impl PartialEq for OrderedMap

Source§

fn eq(&self, other: &Self) -> bool

Content comparison: same number of entries, and every key in self maps to an equal value in other. Order-insensitive by construction (a lookup by key, not a positional walk) — the len check is a fast path (mismatched sizes can never be equal, and it makes the same-length-different-keys case cheap to reject), and the per-entry get gives each comparison the same Value::eq Arc::ptr_eq shortcuts as any other structural compare. O(n) entries, each a linear getO(n^2) worst case, the accepted trade for small, game-scale maps (same trade get/insert/remove already make).

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for OrderedMap

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = !

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.