Struct near_sdk::collections::LazyOption
source · pub struct LazyOption<T> { /* private fields */ }
Expand description
An persistent lazy option, that stores a value in the storage.
Implementations
sourceimpl<T> LazyOption<T>
impl<T> LazyOption<T>
sourceimpl<T> LazyOption<T>where
T: BorshSerialize + BorshDeserialize,
impl<T> LazyOption<T>where
T: BorshSerialize + BorshDeserialize,
sourcepub fn new<S>(storage_key: S, value: Option<&T>) -> Selfwhere
S: IntoStorageKey,
pub fn new<S>(storage_key: S, value: Option<&T>) -> Selfwhere
S: IntoStorageKey,
Create a new lazy option with the given storage_key
and the initial value.
Examples
use near_sdk::collections::LazyOption;
let option: LazyOption<u32> = LazyOption::new(b"l", Some(&42));
let another_option: LazyOption<u32> = LazyOption::new(b"l", None);
sourcepub fn remove(&mut self) -> bool
pub fn remove(&mut self) -> bool
Removes the value from storage without reading it. Returns whether the value was present.
Examples
use near_sdk::collections::LazyOption;
let mut option: LazyOption<u32> = LazyOption::new(b"l", Some(&42));
assert_eq!(option.remove(), true);
assert!(option.is_none());
assert_eq!(option.remove(), false);
sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Removes the value from storage and returns it as an option.
Examples
use near_sdk::collections::LazyOption;
let mut option: LazyOption<u32> = LazyOption::new(b"l", Some(&42));
assert_eq!(option.take(), Some(42));
assert!(option.is_none());
assert_eq!(option.take(), None);
sourcepub fn get(&self) -> Option<T>
pub fn get(&self) -> Option<T>
Gets the value from storage and returns it as an option.
Examples
use near_sdk::collections::LazyOption;
let mut option: LazyOption<u32> = LazyOption::new(b"l", None);
assert_eq!(option.get(), None);
option.set(&42);
assert_eq!(option.get(), Some(42));
assert!(option.is_some());
sourcepub fn set(&mut self, value: &T) -> bool
pub fn set(&mut self, value: &T) -> bool
Sets the value into the storage without reading the previous value and returns whether the previous value was present.
Examples
use near_sdk::collections::LazyOption;
let mut option: LazyOption<u32> = LazyOption::new(b"l", None);
assert_eq!(option.set(&42), false);
assert_eq!(option.set(&420), true);
sourcepub fn replace(&mut self, value: &T) -> Option<T>
pub fn replace(&mut self, value: &T) -> Option<T>
Replaces the value in the storage and returns the previous value as an option.
Examples
use near_sdk::collections::LazyOption;
let mut option: LazyOption<u32> = LazyOption::new(b"l", None);
assert_eq!(option.replace(&42), None);
assert_eq!(option.replace(&420), Some(42));
Trait Implementations
sourceimpl<T> BorshDeserialize for LazyOption<T>where
Vec<u8>: BorshDeserialize,
impl<T> BorshDeserialize for LazyOption<T>where
Vec<u8>: BorshDeserialize,
sourceimpl<T> BorshSerialize for LazyOption<T>where
Vec<u8>: BorshSerialize,
impl<T> BorshSerialize for LazyOption<T>where
Vec<u8>: BorshSerialize,
sourceimpl<T> Debug for LazyOption<T>where
T: Debug + BorshSerialize + BorshDeserialize,
impl<T> Debug for LazyOption<T>where
T: Debug + BorshSerialize + BorshDeserialize,
Auto Trait Implementations
impl<T> RefUnwindSafe for LazyOption<T>where
T: RefUnwindSafe,
impl<T> Send for LazyOption<T>where
T: Send,
impl<T> Sync for LazyOption<T>where
T: Sync,
impl<T> Unpin for LazyOption<T>where
T: Unpin,
impl<T> UnwindSafe for LazyOption<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more