Struct LazyOption

Source
pub struct LazyOption<T> { /* private fields */ }
Expand description

An persistent lazy option, that stores a value in the storage.

Implementations§

Source§

impl<T> LazyOption<T>

Source

pub fn is_some(&self) -> bool

Returns true if the value is present in the storage.

Source

pub fn is_none(&self) -> bool

Returns true if the value is not present in the storage.

Source§

impl<T> LazyOption<T>

Source

pub fn new<S>(storage_key: S, value: Option<&T>) -> Self
where S: IntoStorageKey,

Create a new lazy option with the given storage_key and the initial value.

§Examples
use unc_sdk::collections::LazyOption;

let option: LazyOption<u32> = LazyOption::new(b"l", Some(&42));
let another_option: LazyOption<u32> = LazyOption::new(b"l", None);
Source

pub fn remove(&mut self) -> bool

Removes the value from storage without reading it. Returns whether the value was present.

§Examples
use unc_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);
Source

pub fn take(&mut self) -> Option<T>

Removes the value from storage and returns it as an option.

§Examples
use unc_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);
Source

pub fn get(&self) -> Option<T>

Gets the value from storage and returns it as an option.

§Examples
use unc_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());
Source

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 unc_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);
Source

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

Source§

impl<T> BorshDeserialize for LazyOption<T>

Source§

fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl<T> BorshSerialize for LazyOption<T>

Source§

fn serialize<__W: Write>(&self, writer: &mut __W) -> Result<(), Error>

Source§

impl<T> Debug for LazyOption<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LazyOption<T>

§

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§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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