Skip to main content

OrderedMap

Struct OrderedMap 

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

A map from String to V that remembers the order its keys arrived in.

use structio::OrderedMap;

let mut map = OrderedMap::new();
map.insert("zeta".to_string(), 1);
map.insert("alpha".to_string(), 2);
map.insert("zeta".to_string(), 3); // replaces, keeps position

assert_eq!(map.get("zeta"), Some(&3));
assert_eq!(
    map.keys().map(String::as_str).collect::<Vec<_>>(),
    ["zeta", "alpha"]
);

See the module docs for how lookups work and what the ordering does and does not extend to.

Implementations§

Source§

impl<V> OrderedMap<V>

Source

pub fn new() -> Self

An empty map. Allocates nothing.

Source

pub fn with_capacity(capacity: usize) -> Self

An empty map with room for capacity entries.

The table is not allocated here, however large the capacity. A table that exists describes every entry, so one built ahead of the entries would have to be maintained through the first eight inserts that the whole point of LINEAR_MAX is to leave alone; the map builds it at the entry that first needs it, sized from the entries it actually holds.

Source

pub fn capacity(&self) -> usize

How many entries the map can hold before it reallocates.

Source

pub fn reserve(&mut self, additional: usize)

Reserve room for additional more entries.

Source

pub fn len(&self) -> usize

The number of entries.

Source

pub fn is_empty(&self) -> bool

Whether the map holds nothing.

Source

pub fn clear(&mut self)

Drop every entry, keeping the entries’ allocated capacity.

The table goes, since an empty map scans; a map refilled to past LINEAR_MAX builds a new one.

Source

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

A reference to the value key maps to.

Source

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

A mutable reference to the value key maps to.

The key itself is never handed out mutably: changing it would move the entry to a different place in the index without moving the slot that points at it.

Source

pub fn get_key_value(&self, key: &str) -> Option<(&str, &V)>

The stored key and its value.

The key comes back as &str rather than &String, as it does from every other accessor here; only Self::iter hands out &String, because the writers take their entries that way.

Source

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

Whether key is present.

Source

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

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

A duplicate key keeps the position it already had: the map records where a key first appeared, and writing to it again is not a reason to move it. This is the one difference from BTreeMap::insert, which has no position to keep.

There is no separate duplicate check to pay for. The probe that would find an existing key is the same probe that finds the bucket a new one goes in, so an insert costs one walk of the table either way.

Source

pub fn remove(&mut self, key: &str) -> Option<V>

Remove key, returning its value.

Entries after it shift down to close the gap, which is O(n) and is what keeps the order intact. A swap with the last entry would be O(1) and would scramble exactly the thing this map exists to preserve.

Source

pub fn remove_entry(&mut self, key: &str) -> Option<(String, V)>

Remove key, returning the stored key along with its value.

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&str, &mut V) -> bool,

Keep only the entries f returns true for, in order.

Source

pub fn sort_keys(&mut self)

Reorder the entries by key.

The escape hatch for a caller who wants a BTreeMap’s output: sort once, deliberately, instead of paying for an ordering on every read. The order the map was built in is gone afterwards.

Source

pub fn get_index(&self, index: usize) -> Option<(&str, &V)>

The key and value at position index, in insertion order.

Source

pub fn get_index_mut(&mut self, index: usize) -> Option<(&str, &mut V)>

The key and mutable value at position index, in insertion order.

Source

pub fn first(&self) -> Option<(&str, &V)>

The first entry, or None if the map is empty.

Source

pub fn last(&self) -> Option<(&str, &V)>

The last entry, or None if the map is empty.

Source

pub fn entry(&mut self, key: String) -> Entry<'_, V>

The entry for key, occupied or vacant.

Takes an owned key because a vacant entry will store it, and looking up by &str first only to allocate the same string afterwards is the cost this avoids.

Source

pub fn iter(&self) -> Iter<'_, V> ⓘ

The entries, in insertion order.

Yields (&String, &V) rather than &(String, V), which is the shape the JSON and BEVE writers take their keyed entries in, so a map goes straight into one.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, V> ⓘ

The entries with mutable values, in insertion order.

Source

pub fn keys(&self) -> Keys<'_, V> ⓘ

The keys, in insertion order.

Source

pub fn values(&self) -> Values<'_, V> ⓘ

The values, in insertion order.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, V> ⓘ

The values, mutably, in insertion order.

Trait Implementations§

Source§

impl<V: Clone> Clone for OrderedMap<V>

Source§

fn clone(&self) -> Self

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<V: Debug> Debug for OrderedMap<V>

Source§

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

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

impl<V> Default for OrderedMap<V>

Source§

fn default() -> Self

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

impl<V: Eq> Eq for OrderedMap<V>

Sound because keys are unique: equal lengths plus a value-for-value match in one direction is a bijection, so the relation is symmetric and transitive.

Source§

impl<V> Extend<(String, V)> for OrderedMap<V>

Source§

fn extend<I: IntoIterator<Item = (String, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<OrderedMap<Value>> for Value

Source§

fn from(v: Object) -> Self

Converts to this type from the input type.
Source§

impl<V, const N: usize> From<[(String, V); N]> for OrderedMap<V>

Source§

fn from(entries: [(String, V); N]) -> Self

Converts to this type from the input type.
Source§

impl<V> FromIterator<(String, V)> for OrderedMap<V>

Source§

fn from_iter<I: IntoIterator<Item = (String, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<V> Index<&str> for OrderedMap<V>

Source§

fn index(&self, key: &str) -> &V

§Panics

If the key is not present. Use OrderedMap::get for a map that may not have it.

Source§

type Output = V

The returned type after indexing.
Source§

impl<V> IntoIterator for OrderedMap<V>

Source§

type Item = (String, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, V> IntoIterator for &'a OrderedMap<V>

Source§

type Item = (&'a String, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, V> IntoIterator for &'a mut OrderedMap<V>

Source§

type Item = (&'a String, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V: PartialEq> PartialEq for OrderedMap<V>

Equality is over the entries, not over their order: see the module docs.

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'de, V: Read<'de> + Default> Read<'de> for OrderedMap<V>

OrderedMap keeps the order its members arrived in, which is what makes it the map behind Object.

Hand-written rather than an arm of impl_map!: that macro is generic over the key type, and this map’s key is always a String. It reserves on the member count, as the hashed map does, since its entries live in one vector.

Source§

fn read<O: Options>(&mut self, r: &mut Reader<'de, O>) -> Result<(), ErrorCode>

Read into self, from the reader’s current position. Read more
Source§

fn read_bulk<O: Options>( _out: &mut Vec<Self>, _n: usize, _elem: u8, _r: &mut Reader<'de, O>, ) -> Result<bool, ErrorCode>

Fill out from the payload of a typed array of n elements whose element header is elem, or return false to decline. Read more
Source§

impl<'de, V: Read<'de> + Default> Read<'de> for OrderedMap<V>

OrderedMap keeps the order its members arrived in, which is what makes it the map behind Object.

Hand-written rather than an arm of impl_map!: that macro is generic over the key type, and this map’s key is always a String.

Source§

fn read<O: Options>(&mut self, p: &mut Parser<'de, O>) -> Result<(), ErrorCode>

Parse into self, from the cursor’s current position. Read more
Source§

impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>
where VA: ReadAs<'de, V>,

OrderedMap’s adapter form takes one adapter rather than two, since its key type is fixed: the object header is always the string-keyed one, and the keys go through String’s own FromBeveKey/ToBeveKey.

Source§

fn read<O: Options>( value: &mut OrderedMap<V>, r: &mut Reader<'de, O>, ) -> Result<(), ErrorCode>

Read into value, from the reader’s current position. Read more
Source§

fn read_bulk<O: Options>( _out: &mut Vec<T>, _n: usize, _elem: u8, _r: &mut Reader<'de, O>, ) -> Result<bool, ErrorCode>

Fill out from the payload of a typed array of n elements whose element header is elem, or return false to decline. Read more
Source§

impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>
where VA: ReadAs<'de, V>,

OrderedMap’s adapter form takes one adapter rather than two, since its key type is fixed: OrderedMap<Millis> adapts the values and leaves the keys to String’s own FromJsonKey/ToJsonKey.

Source§

fn read<O: Options>( value: &mut OrderedMap<V>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Read into value, from the cursor’s current position. Read more
Source§

impl<V: Write> Write for OrderedMap<V>

Source§

fn write<O: Options>(&self, w: &mut Writer<'_, O>)

Source§

fn is_null(&self) -> bool

Whether this value is absent, and so is left out of an object under Options::SKIP_NULL. Read more
Source§

const ARRAY: Option<&'static [u8]> = None

The header bytes a contiguous run of this type is stored under, or None if it has no array form of its own and belongs in a generic one. Read more
Source§

fn write_payload<O: Options>(items: &[Self], w: &mut Writer<'_, O>)
where Self: Sized,

Append items as the payload of a typed array whose header and count the caller has already written. Read more
Source§

impl<V: Write> Write for OrderedMap<V>

Source§

fn write<O: Options>(&self, w: &mut Writer<'_, O>)

Source§

fn is_null(&self) -> bool

Whether this value is absent, and so is left out of an object under Options::SKIP_NULL. Read more
Source§

impl<VA, V> WriteAs<OrderedMap<V>> for OrderedMap<VA>
where VA: WriteAs<V>,

Source§

fn write<O: Options>(value: &OrderedMap<V>, w: &mut Writer<'_, O>)

Write value. Read more
Source§

fn is_null(value: &T) -> bool

Whether the field is absent, and so is left out of an object under Options::SKIP_NULL. Read more
Source§

const ARRAY: Option<&'static [u8]> = None

The adapter’s answer to Write::ARRAY: the header bytes a contiguous run of adapted values is stored under, or None for a generic array. Read more
Source§

fn write_payload<O: Options>(items: &[T], w: &mut Writer<'_, O>)
where T: Sized,

Append items as the payload of a typed array whose header and count the caller has already written. Read more
Source§

impl<VA, V> WriteAs<OrderedMap<V>> for OrderedMap<VA>
where VA: WriteAs<V>,

Source§

fn write<O: Options>(value: &OrderedMap<V>, w: &mut Writer<'_, O>)

Write value. Read more
Source§

fn is_null(value: &T) -> bool

Whether the field is absent, and so is left out of an object under Options::SKIP_NULL. Read more

Auto Trait Implementations§

§

impl<V> Freeze for OrderedMap<V>
where Vec<(String, V)>: Freeze,

§

impl<V> RefUnwindSafe for OrderedMap<V>
where Vec<(String, V)>: RefUnwindSafe,

§

impl<V> Send for OrderedMap<V>
where Vec<(String, V)>: Send,

§

impl<V> Sync for OrderedMap<V>
where Vec<(String, V)>: Sync,

§

impl<V> Unpin for OrderedMap<V>
where Vec<(String, V)>: Unpin,

§

impl<V> UnsafeUnpin for OrderedMap<V>
where Vec<(String, V)>: UnsafeUnpin,

§

impl<V> UnwindSafe for OrderedMap<V>
where Vec<(String, V)>: UnwindSafe,

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> 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> ReadOwned for T
where T: Default + for<'de> Read<'de>,

Source§

impl<T> ReadOwned for T
where T: Default + for<'de> Read<'de>,

Source§

impl<T> ReadOwned for T
where T: ReadOwned + ReadOwned,

Source§

impl<T> ReadWrite for T
where T: for<'de> Read<'de> + Write,

Source§

impl<T> ReadWrite for T
where T: for<'de> Read<'de> + Write,

Source§

impl<T> ReadWrite for T
where T: ReadWrite + ReadWrite,

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

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<T> Write for T
where T: Write + Write + ?Sized,