Struct fixed_map::Set

source ·
pub struct Set<T>where
    T: Key,
{ /* private fields */ }
Expand description

A fixed set with storage specialized through the Key trait.

Examples

use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Part {
    One,
    Two,
}

#[derive(Clone, Copy, Key)]
enum Key {
    Simple,
    Composite(Part),
    String(&'static str),
    Number(u32),
    Singleton(()),
    Option(Option<Part>),
    Boolean(bool),
}

let mut set = Set::new();

set.insert(Key::Simple);
set.insert(Key::Composite(Part::One));
set.insert(Key::String("foo"));
set.insert(Key::Number(1));
set.insert(Key::Singleton(()));
set.insert(Key::Option(None));
set.insert(Key::Option(Some(Part::One)));
set.insert(Key::Boolean(true));

assert!(set.contains(Key::Simple));
assert!(set.contains(Key::Composite(Part::One)));
assert!(!set.contains(Key::Composite(Part::Two)));
assert!(set.contains(Key::String("foo")));
assert!(!set.contains(Key::String("bar")));
assert!(set.contains(Key::Number(1)));
assert!(!set.contains(Key::Number(2)));
assert!(set.contains(Key::Singleton(())));
assert!(set.contains(Key::Option(None)));
assert!(set.contains(Key::Option(Some(Part::One))));
assert!(!set.contains(Key::Option(Some(Part::Two))));
assert!(set.contains(Key::Boolean(true)));
assert!(!set.contains(Key::Boolean(false)));

Implementations§

A set implementation that uses fixed storage.

Examples

use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut m = Set::new();
m.insert(Key::First);

assert_eq!(m.contains(Key::First), true);
assert_eq!(m.contains(Key::Second), false);
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Part {
    A,
    B,
}

#[derive(Clone, Copy, Key)]
enum Key {
    Simple,
    Composite(Part),
}

let mut m = Set::new();
m.insert(Key::Simple);
m.insert(Key::Composite(Part::A));

assert_eq!(m.contains(Key::Simple), true);
assert_eq!(m.contains(Key::Composite(Part::A)), true);
assert_eq!(m.contains(Key::Composite(Part::B)), false);

Creates an empty Set.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let set: Set<Key> = Set::new();

An iterator visiting all values in arbitrary order. The iterator element type is T.

Examples
use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    One,
    Two,
    Three,
}

let mut set = Set::new();
set.insert(Key::One);
set.insert(Key::Two);

assert_eq!(set.iter().collect::<Vec<_>>(), vec![Key::One, Key::Two]);

Returns true if the set currently contains the given value.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut set = Set::new();
set.insert(Key::One);
assert_eq!(set.contains(Key::One), true);
assert_eq!(set.contains(Key::Two), false);

Adds a value to the set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut set = Set::new();
assert!(set.insert(Key::One));
assert!(!set.is_empty());

set.insert(Key::Two);
assert!(!set.insert(Key::Two));
assert!(set.contains(Key::Two));

Removes a value from the set. Returns true if the value was present in the set.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut set = Set::new();
set.insert(Key::One);
assert_eq!(set.remove(Key::One), true);
assert_eq!(set.remove(Key::One), false);

Retains only the elements specified by the predicate.

In other words, remove all elements e for which f(e) returns false. The elements are visited in unsorted (and unspecified) order.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut set = Set::new();

set.insert(Key::First);
set.insert(Key::Second);

set.retain(|k| matches!(k, Key::First));

assert_eq!(set.len(), 1);
assert_eq!(set.contains(Key::First), true);
assert_eq!(set.contains(Key::Second), false);

Using a composite key:

use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    First(bool),
    Second(bool),
}

let mut set = Set::new();

set.insert(Key::First(true));
set.insert(Key::First(false));
set.insert(Key::Second(true));
set.insert(Key::Second(false));

let mut other = set.clone();
assert_eq!(set.len(), 4);

set.retain(|k| matches!(k, Key::First(true) | Key::Second(true)));

assert_eq!(set.len(), 2);
assert_eq!(set.contains(Key::First(true)), true);
assert_eq!(set.contains(Key::First(false)), false);
assert_eq!(set.contains(Key::Second(true)), true);
assert_eq!(set.contains(Key::Second(false)), false);

other.retain(|k| matches!(k, Key::First(_)));

assert_eq!(other.len(), 2);
assert_eq!(other.contains(Key::First(true)), true);
assert_eq!(other.contains(Key::First(false)), true);
assert_eq!(other.contains(Key::Second(true)), false);
assert_eq!(other.contains(Key::Second(false)), false);

Clears the set, removing all values.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut set = Set::new();
set.insert(Key::One);
set.clear();
assert!(set.is_empty());

Returns true if the set contains no elements.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    One,
    Two,
}

let mut set = Set::new();
assert!(set.is_empty());
set.insert(Key::One);
assert!(!set.is_empty());

Returns the number of elements in the set.

Examples
use fixed_map::{Key, Set};

#[derive(Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut set = Set::new();
assert_eq!(set.len(), 0);
set.insert(Key::First);
assert_eq!(set.len(), 1);

Trait Implementations§

Clone implementation for a Set.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Set::new();
a.insert(Key::First(true));
let mut b = a.clone();
b.insert(Key::Second);

assert_ne!(a, b);

assert!(a.contains(Key::First(true)));
assert!(!a.contains(Key::Second));

assert!(b.contains(Key::First(true)));
assert!(b.contains(Key::Second));
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

The Debug implementation for a Set.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut a = Set::new();
a.insert(Key::First);

assert_eq!("{First}", format!("{:?}", a));
Formats the value using the given formatter. Read more

The Default implementation for a Set produces an empty set.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let a = Set::<Key>::default();
let b = Set::<Key>::new();

assert_eq!(a, b);
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Creates a value from an iterator. Read more

Hash implementation for a Set.

Examples

use std::collections::HashSet;

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First,
    Second,
}

let mut a = Set::new();
a.insert(Key::First);

let mut set = HashSet::new();
set.insert(a);

Using a composite key:

use std::collections::HashSet;

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Set::new();
a.insert(Key::First(true));

// TODO: support this
// let mut set = HashSet::new();
// set.insert(a);
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Produce an owning iterator which iterates over all elements in the set in order.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    First,
    Second,
    Third,
}

let mut set = Set::new();
set.insert(Key::First);
set.insert(Key::Second);

assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![Key::First, Key::Second]);

An iterator visiting all values in arbitrary order. The iterator element type is T.

Examples
use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Key)]
enum Key {
    One,
    Two,
    Three,
}

let mut set = Set::new();
set.insert(Key::One);
set.insert(Key::Two);

assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![Key::One, Key::Two]);
The type of the elements being iterated over.
Which kind of iterator are we turning this into?

Ord implementation for a Set.

For more details on ordering, see the Key documentation.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First,
    Second,
}

let mut a = Set::new();
a.insert(Key::First);

let mut b = Set::new();
b.insert(Key::Second);

let mut list = vec![b, a];
list.sort();

assert_eq!(list, [a, b]);

Using a composite key:

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Set::new();
a.insert(Key::First(true));

let mut b = Set::new();
b.insert(Key::Second);

// TODO: support this
// let mut list = vec![a, b];
// list.sort();
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more

PartialEq implementation for a Set.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut a = Set::new();
a.insert(Key::First);
// Note: `a` is Copy since it's using a simple key.
let mut b = a;

assert_eq!(a, b);

b.insert(Key::Second);
assert_ne!(a, b);

Using a composite key:

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Set::new();
a.insert(Key::First(true));
let mut b = a.clone();

assert_eq!(a, b);

b.insert(Key::Second);
assert_ne!(a, b);
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

PartialOrd implementation for a Set.

For more details on ordering, see the Key documentation.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First,
    Second,
    Third,
}

let mut a = Set::new();
a.insert(Key::First);

let mut b = Set::new();
b.insert(Key::Third);

assert!(a < b);

let mut empty = Set::new();
assert!(empty < a);
assert!(empty < b);

Using a composite key:

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Hash, Key)]
enum Key {
    First(bool),
    Second,
}

let mut a = Set::new();
a.insert(Key::First(true));

let mut b = Set::new();
b.insert(Key::Second);

// TODO: support this
// assert!(a < b);
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

The Copy implementation for a Set depends on its Key. If the derived key only consists of unit variants the corresponding Set will be Copy as well.

Examples

use fixed_map::{Key, Set};

#[derive(Debug, Clone, Copy, Key)]
enum Key {
    First,
    Second,
}

let mut a = Set::new();
a.insert(Key::First);
let mut b = a;
b.insert(Key::Second);

assert_ne!(a, b);

assert!(a.contains(Key::First));
assert!(!a.contains(Key::Second));

assert!(b.contains(Key::First));
assert!(b.contains(Key::Second));

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.