Skip to main content

Counter

Struct Counter 

Source
pub struct Counter { /* private fields */ }
Expand description

A counter at one key.

Sugar over crate::keyspace::Strings, and the kind of sugar that is worth having: counting is the commonest thing a string key is used for, and a handle that holds the key means the key is spelled once instead of at every call site, which is one fewer place for a typo to live.

A counter that has never been written reads as zero, the way Redis’s does, so there is no create step and nothing to check before the first incr.

let db = yo::open(yo::MEMORY)?;
let hits = db.counter("hits");

assert_eq!(hits.get()?, 0);
assert_eq!(hits.incr()?, 1);
assert_eq!(hits.add(41)?, 42);

Implementations§

Source§

impl Counter

Source

pub fn key(&self) -> &[u8]

The key this counter lives at.

Source

pub fn get(&self) -> Result<i64>

What the counter reads, which is zero if nothing has written it yet.

§Errors

Code::Invalid if the key holds something that is not an integer.

Source

pub fn incr(&self) -> Result<i64>

Add one and hand back the result. INCR.

§Errors

Code::Invalid if the key holds something that is not an integer, or if the result would leave the range of an i64.

Source

pub fn decr(&self) -> Result<i64>

Subtract one and hand back the result. DECR.

§Errors

As Counter::incr.

Source

pub fn add(&self, by: i64) -> Result<i64>

Add by and hand back the result, which is DECRBY for a negative by. INCRBY.

§Errors

As Counter::incr.

Source

pub fn set(&self, value: i64) -> Result<()>

Put the counter at value, whatever it read before. SET.

§Errors

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

Source

pub fn reset(&self) -> Result<()>

Remove the key, so the counter reads zero again and stops taking up room. DEL.

§Errors

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

Trait Implementations§

Source§

impl Clone for Counter

Source§

fn clone(&self) -> Counter

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 Debug for Counter

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.