Skip to main content

Set

Struct Set 

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

One set, with its key held for you.

This is 15 section 2’s shape: the name is spelled where the handle is made and nowhere else, so a typo is one compile error at one line instead of a lookup that quietly misses at three call sites.

let db = yo::open(yo::MEMORY)?;
let online = db.set("online");

online.add("alice")?;
online.add("bob")?;
assert!(online.contains("alice")?);
assert_eq!(online.len()?, 2);

online.remove("alice")?;
assert!(!online.contains("alice")?);

Implementations§

Source§

impl Set

Source

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

The key this handle holds.

Source

pub fn add(&self, member: impl AsRef<[u8]>) -> Result<bool>

Add one member, and say whether it was new. SADD.

§Errors

As Sets::add.

Source

pub fn add_many<M: AsRef<[u8]>>(&self, members: &[M]) -> Result<usize>

Add several members, and say how many were new.

§Errors

As Sets::add.

Source

pub fn remove(&self, member: impl AsRef<[u8]>) -> Result<bool>

Remove one member, and say whether it was there. SREM.

§Errors

As Sets::add.

Source

pub fn remove_many<M: AsRef<[u8]>>(&self, members: &[M]) -> Result<usize>

Remove several members, and say how many were there.

§Errors

As Sets::add.

Source

pub fn contains(&self, member: impl AsRef<[u8]>) -> Result<bool>

Whether a member is in the set. SISMEMBER.

§Errors

As Sets::add.

Source

pub fn contains_many<M: AsRef<[u8]>>(&self, members: &[M]) -> Result<Vec<bool>>

Whether each of several members is in the set, in the order asked. SMISMEMBER.

§Errors

As Sets::add.

Source

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

How many members it holds. SCARD.

§Errors

As Sets::add.

Source

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

Whether it holds nothing, which is also true of a key that is not there.

§Errors

As Sets::add.

Source

pub fn members(&self) -> Result<Vec<Vec<u8>>>

Every member, owned. SMEMBERS.

An empty Vec for a key that is not there. Sets::members is the version that tells the two apart, and this one does not because a handle on a key that was never written to is the ordinary way to start.

§Errors

As Sets::add.

Source

pub fn for_each(&self, f: impl FnMut(Member<'_>)) -> Result<()>

Hand every member to f where it lies, allocating nothing.

§Errors

As Sets::add.

Source

pub fn pop(&self) -> Result<Option<Vec<u8>>>

Take one member out at random. SPOP.

§Errors

As Sets::add.

Source

pub fn pop_n(&self, count: usize) -> Result<Vec<Vec<u8>>>

Take up to count distinct members out at random. SPOP key count.

§Errors

As Sets::add.

Source

pub fn pick(&self) -> Result<Option<Vec<u8>>>

Draw one member and leave it in the set. SRANDMEMBER.

§Errors

As Sets::add.

Source

pub fn pick_n(&self, count: i64) -> Result<Vec<Vec<u8>>>

Draw count members and leave them in the set. SRANDMEMBER key count.

§Errors

As Sets::add.

Source

pub fn move_to(&self, to: &Set, member: impl AsRef<[u8]>) -> Result<bool>

Move one member into another set, and say whether it moved. SMOVE.

§Errors

As Sets::add, and Code::Invalid when to belongs to a different database.

Source

pub fn intersect(&self, others: &[&Set]) -> Result<Vec<Vec<u8>>>

Everything in this set and in all of others. SINTER.

let db = yo::open(yo::MEMORY)?;
let a = db.set("a");
let b = db.set("b");
a.add_many(&["x", "y"])?;
b.add_many(&["y", "z"])?;

assert_eq!(a.intersect(&[&b])?, vec![b"y".to_vec()]);
§Errors

As Set::move_to.

Source

pub fn intersect_len(&self, others: &[&Set], limit: usize) -> Result<usize>

How many members this set shares with all of others. SINTERCARD.

A limit of zero means no limit.

§Errors

As Set::move_to.

Source

pub fn union(&self, others: &[&Set]) -> Result<Vec<Vec<u8>>>

Everything in this set or in any of others. SUNION.

§Errors

As Set::move_to.

Source

pub fn union_len(&self, others: &[&Set], limit: usize) -> Result<usize>

How many members this set and others have between them. SUNIONCARD.

A limit of zero means no limit.

§Errors

As Set::move_to.

Source

pub fn difference(&self, others: &[&Set]) -> Result<Vec<Vec<u8>>>

Everything in this set and in none of others. SDIFF.

§Errors

As Set::move_to.

Source

pub fn difference_len(&self, others: &[&Set], limit: usize) -> Result<usize>

How many members this set has that no set in others has. SDIFFCARD.

A limit of zero means no limit.

§Errors

As Set::move_to.

Source

pub fn intersect_into( &self, destination: &Set, others: &[&Set], ) -> Result<usize>

Store the intersection in destination and say how big it is. SINTERSTORE.

§Errors

As Set::move_to.

Source

pub fn union_into(&self, destination: &Set, others: &[&Set]) -> Result<usize>

Store the union in destination and say how big it is. SUNIONSTORE.

§Errors

As Set::move_to.

Source

pub fn difference_into( &self, destination: &Set, others: &[&Set], ) -> Result<usize>

Store the difference in destination and say how big it is. SDIFFSTORE.

§Errors

As Set::move_to.

Source

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

Remove the whole set, and say whether it was there. DEL.

§Errors

As Sets::add.

Trait Implementations§

Source§

impl Clone for Set

Source§

fn clone(&self) -> Set

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 Set

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Set

§

impl !Send for Set

§

impl !Sync for Set

§

impl !UnwindSafe for Set

§

impl Freeze for Set

§

impl Unpin for Set

§

impl UnsafeUnpin for Set

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, !>

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.