Struct Set

Source
pub struct Set<T: Eq + Hash = Key> { /* private fields */ }
Expand description

A hash set implemented as a Map where the value is ().

Implementations§

Source§

impl<T: Eq + Hash> Set<T>

Source

pub fn new() -> Self

Creates an empty Set.

§Example
use json_api::value::{Key, Set};
let mut set = Set::<Key>::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new empty Set, with specified capacity.

§Example
let mut set = Set::with_capacity(2);

set.insert("x");
set.insert("y");

// The next insert will likely require reallocation...
set.insert("z");
Source

pub fn capacity(&self) -> usize

Returns the number of elements the set can hold without reallocating.

§Example
let set = Set::<Key>::with_capacity(2);
assert!(set.capacity() >= 2);
Source

pub fn clear(&mut self)

Clears the set, removing all elements. Keeps the allocated memory for reuse.

§Example
let mut set = Set::new();

set.insert("x");
set.clear();
assert!(set.is_empty());
Source

pub fn contains<Q>(&self, key: &Q) -> bool
where Q: Equivalent<T> + Hash + ?Sized,

Returns true if the set contains the specified value.

§Example
let mut set = Set::new();

set.insert(1);
assert!(set.contains(&1));
assert!(!set.contains(&2));
Source

pub fn drain(&mut self, range: RangeFull) -> Drain<'_, T>

Clears the set, returning all elements in an iterator. Keeps the allocated memory for reuse.

§Example
let mut set = Set::new();

set.insert(1);
set.insert(2);

for item in set.drain(..) {
    assert!(item == 1 || item == 2);
}

assert!(set.is_empty());
Source

pub fn insert(&mut self, key: T) -> bool

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.

§Example
let mut set = Set::new();

assert_eq!(set.insert(1), true);
assert_eq!(set.insert(1), false);
assert_eq!(set.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the set does not contain any elements.

§Example
let mut set = Set::new();
assert!(set.is_empty());

set.insert("x");
assert!(!set.is_empty());
Source

pub fn iter(&self) -> Iter<'_, T>

Return an iterator visiting all the elements of the set in the order in which they were inserted.

§Example
let mut set = Set::new();

set.insert("a");
set.insert("b");
set.insert("c");

let mut iter = set.iter();

assert_eq!(iter.next(), Some(&"a"));
assert_eq!(iter.next(), Some(&"b"));
assert_eq!(iter.next(), Some(&"c"));
assert_eq!(iter.next(), None);
Source

pub fn len(&self) -> usize

Return the number of elements in the set.

§Example
let mut set = Set::new();
assert_eq!(set.len(), 0);

set.insert("x");
assert_eq!(set.len(), 1);
Source

pub fn remove<Q>(&mut self, key: &Q) -> bool
where Q: Equivalent<T> + Hash + ?Sized,

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

§Example
let mut set = Set::new();

set.insert("x");

assert!(set.remove("x"));
assert!(!set.remove("x"));
assert_eq!(set.len(), 0);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the Set. The collection may reserve more space to avoid frequent reallocations.

§Note

This method has yet to be fully implemented in the ordermap crate.

§Example
let mut set = Set::<String>::new();
set.reserve(10);

Trait Implementations§

Source§

impl<T: Clone + Eq + Hash> Clone for Set<T>

Source§

fn clone(&self) -> Set<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Eq + Hash> Debug for Set<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Eq + Hash> Default for Set<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T> Deserialize<'de> for Set<T>
where T: Deserialize<'de> + Eq + Hash + 'de,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Eq + Hash> Extend<T> for Set<T>

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Eq + Hash> FromIterator<T> for Set<T>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<T, E> FromStr for Set<T>
where T: Eq + FromStr<Err = E> + Hash, E: Into<Error>,

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(value: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<'a, T: Eq + Hash> IntoIterator for &'a Set<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Eq + Hash> IntoIterator for Set<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<Set<T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq + Eq + Hash> PartialEq for Set<T>

Source§

fn eq(&self, other: &Set<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq + Hash + Serialize> Serialize for Set<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq + Hash + Stringify> Stringify for Set<T>

Source§

fn to_bytes(&self) -> Vec<u8>

Returns the string representation of the give value as a byte vector. Read more
Source§

fn stringify(&self) -> String

Returns the string representation of the given value. Read more
Source§

impl<T: Eq + Eq + Hash> Eq for Set<T>

Source§

impl<T: Eq + Hash> StructuralPartialEq for Set<T>

Auto Trait Implementations§

§

impl<T> Freeze for Set<T>

§

impl<T> RefUnwindSafe for Set<T>
where T: RefUnwindSafe,

§

impl<T> Send for Set<T>
where T: Send,

§

impl<T> Sync for Set<T>
where T: Sync,

§

impl<T> Unpin for Set<T>
where T: Unpin,

§

impl<T> UnwindSafe for Set<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> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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 = 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,