Skip to main content

Map

Struct Map 

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

A map from K to V.

Cheap to clone and cheap to keep around: the handle is a pointer and an index, so cloning one does not copy anything and every clone is the same collection.

The type parameters are not decoration. They are the collection’s shape (15 section 3), they are written down when it is created, and they are what a later open is checked against.

§Borrowed lookups

A lookup takes a borrowed form of the key, the same way HashMap does, so a Map<String, u64> is read with map.get("home") and not with a String built for the length of one call.

let db = yo::open(yo::MEMORY)?;
let hits = db.map::<String, u64>("hits")?;

hits.set("home", &1)?;
assert_eq!(hits.get("home")?, Some(1));
assert_eq!(hits.get("about")?, None);

Implementations§

Source§

impl<K: Decode, V: Decode> Map<K, V>

Source

pub fn name(&self) -> Result<String>

The name this collection was opened under.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn tag(&self) -> Tag

This collection’s shape tag.

The same 128 bits that a file carries and that another language’s binding computes for the same type. Carried in the handle rather than read out of the database, so it costs nothing and cannot fail.

Source

pub fn get<Q>(&self, key: &Q) -> Result<Option<V>>
where K: Borrow<Q>, Q: Encode + ?Sized,

Read a value, owned.

One allocation for the value, and none for the key. When even that one is too many, Map::with hands over the bytes where they lie.

§Errors

Code::Corrupt if the stored bytes are not a V, which means the file disagrees with its own shape.

Source

pub fn with<Q, R>( &self, key: &Q, f: impl FnOnce(V::Ref<'_>) -> R, ) -> Result<Option<R>>
where K: Borrow<Q>, Q: Encode + ?Sized,

Read a value without copying it, by handing the borrowed view to f.

This is Y29 in one method: zero copy is always available and never mandatory. The view points into the arena, so nothing is allocated and nothing is decoded beyond checking that the bytes are a V. It is also where G6’s point read budget is spent, which is why the closure takes the view rather than the value.

f runs while the database is borrowed, so it cannot call back into the same database. One that tries gets Code::Invalid rather than a panic.

let db = yo::open(yo::MEMORY)?;
let names = db.map::<u64, String>("names")?;
names.set(&7, "ada")?;

// No String is built here, and no bytes are copied.
let len = names.with(&7, str::len)?;
assert_eq!(len, Some(3));
§Errors

Code::Corrupt if the stored bytes are not a V.

Source

pub fn set<Q, W>(&self, key: &Q, value: &W) -> Result<()>
where K: Borrow<Q>, Q: Encode + ?Sized, V: Borrow<W>, W: Encode + ?Sized,

Store a value, replacing whatever was there.

The value is taken borrowed as well as the key, so a Map<String, String> is written with map.set("k", "v").

§Errors

Code::Full if the key and the value together are larger than Map::max_entry. A value that big belongs in the log region, which arrives with the file format in M5.

Source

pub fn del<Q>(&self, key: &Q) -> Result<bool>
where K: Borrow<Q>, Q: Encode + ?Sized,

Remove a key, returning whether it was there.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn contains<Q>(&self, key: &Q) -> Result<bool>
where K: Borrow<Q>, Q: Encode + ?Sized,

Whether a key is present, without reading its value.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn len(&self) -> Result<usize>

How many keys are stored.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn is_empty(&self) -> Result<bool>

Whether the collection is empty.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub const fn max_entry() -> usize

The largest key and value this collection takes, the two together.

Trait Implementations§

Source§

impl<K, V> Clone for Map<K, V>

Source§

fn clone(&self) -> Map<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
Source§

impl<K, V> Debug for Map<K, V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for Map<K, V>

§

impl<K, V> !Send for Map<K, V>

§

impl<K, V> !Sync for Map<K, V>

§

impl<K, V> !UnwindSafe for Map<K, V>

§

impl<K, V> Freeze for Map<K, V>
where PhantomData<fn() -> (K, V)>: Freeze,

§

impl<K, V> Unpin for Map<K, V>
where PhantomData<fn() -> (K, V)>: Unpin,

§

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

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> 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.