Struct fadroma::storage::TypedKey

source ·
pub struct TypedKey<'a, T: Segment + ?Sized>(_);
Expand description

A strongly-typed key with segments defined by the concrete type which must implement Segment. For typed keys which consist of multiple types use TypedKey2, TypedKey3 and TypedKey4. Constructs the key in order of definition.

Examples

use fadroma::{
    cosmwasm_std::testing::mock_dependencies,
    storage::{Key, ItemSpace, TypedKey2}
};
 
fadroma::namespace!(NumbersNs, b"numbers");
 
// Storage for u64 numbers with a key that consists of b"numbers" + a string + a byte.
const NUMBERS: ItemSpace::<u64, NumbersNs, TypedKey2<String, u8>> = ItemSpace::new();
 
let mut deps = mock_dependencies();
let storage = deps.as_mut().storage;
 
let string_segment = "hello".to_string();
let number_segment = 33u8;
 
NUMBERS.save(storage, (&string_segment, &number_segment), &1).unwrap();
 
// Can also be constructed like this
let key = TypedKey2::from((&string_segment, &number_segment));
NUMBERS.save(storage, key.clone(), &1).unwrap();
 
let mut bytes: Vec<u8> = Vec::with_capacity(key.size());
key.write_segments(&mut bytes);
 
assert_eq!(
    bytes,
    [string_segment.as_bytes(), &number_segment.to_be_bytes()].concat()
);

Trait Implementations§

source§

impl<'a, T: Clone + Segment + ?Sized> Clone for TypedKey<'a, T>

source§

fn clone(&self) -> TypedKey<'a, T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'a, T: Debug + Segment + ?Sized> Debug for TypedKey<'a, T>

source§

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

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

impl<'a, T: Segment + ?Sized> From<&'a T> for TypedKey<'a, T>

source§

fn from(value: &'a T) -> Self

Converts to this type from the input type.
source§

impl<'a, T: Hash + Segment + ?Sized> Hash for TypedKey<'a, T>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, T: Segment + ?Sized> Key for TypedKey<'a, T>

source§

fn size(&self) -> usize

source§

fn write_segments(&self, buf: &mut Vec<u8>)

source§

impl<'a, T: PartialEq + Segment + ?Sized> PartialEq<TypedKey<'a, T>> for TypedKey<'a, T>

source§

fn eq(&self, other: &TypedKey<'a, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

impl<'a, T: Copy + Segment + ?Sized> Copy for TypedKey<'a, T>

source§

impl<'a, T: Segment + ?Sized> StructuralPartialEq for TypedKey<'a, T>

Auto Trait Implementations§

§

impl<'a, T: ?Sized> RefUnwindSafe for TypedKey<'a, T>where T: RefUnwindSafe,

§

impl<'a, T: ?Sized> Send for TypedKey<'a, T>where T: Sync,

§

impl<'a, T: ?Sized> Sync for TypedKey<'a, T>where T: Sync,

§

impl<'a, T: ?Sized> Unpin for TypedKey<'a, T>

§

impl<'a, T: ?Sized> UnwindSafe for TypedKey<'a, T>where T: RefUnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<T> CallHasher for Twhere T: Hash + ?Sized,

§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64where H: Hash + ?Sized, B: BuildHasher,

source§

impl<T> DynClone for Twhere T: Clone,

source§

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

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> Segment for Twhere T: Key,

source§

fn size(&self) -> usize

source§

fn write_segment(&self, buf: &mut Vec<u8, Global>)

source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

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

§

fn vzip(self) -> V