FrozenSet

Struct FrozenSet 

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

A compact frozen (immutable) ordered set backed by a FrozenMap<T, ()>.

This is a read-only set that stores elements in a contiguous, sorted array. It provides efficient binary search lookups and iteration, but cannot be modified after construction.

§Construction

If you’re building a new set, and you don’t expect many overlapping items, consider pushing items into a Vec and calling FrozenSet::from or using the FromIterator implementation via Iterator::collect. It is typically cheaper to collect into a Vec and sort the items once at the end than it is to maintain a temporary set data structure.

If you already have a set, or you have many overlapping items that you don’t want to temporarily hold onto, you can use the From or Into traits to create a FrozenSet from one of many common collections. You should prefer using a BTreeSet, as it matches the sorted semantics of FrozenSet and avoids a sort operation during conversion.

Overlapping items encountered during construction preserve the last overlapping item, matching similar behavior for other sets in the standard library.

Similar to the API of BTreeSet, there are no convenience methods for constructing from a Vec or boxed slice. Because of limitations of the internal representation and Rust’s memory layout rules, the most efficient way to convert from these data structures is via an Iterator.

Implementations§

Source§

impl<T> FrozenSet<T>

Source

pub fn new() -> Self

Creates an empty FrozenSet. Does not perform any heap allocations.

Source§

impl<T> FrozenSet<T>
where T: Ord,

Source

pub fn from_unique_sorted_iter(items: impl IntoIterator<Item = T>) -> Self

Creates a FrozenSet from a pre-sorted iterator with unique items.

This is more efficient than Iterator::collect or FromIterator::from_iter if you know that the iterator is sorted and has no overlapping items.

Panics if the items are not unique and sorted.

Source

pub fn from_unique_sorted_iter_unchecked( items: impl IntoIterator<Item = T>, ) -> Self

Creates a FrozenSet from a pre-sorted iterator with unique items.

This is more efficient than Iterator::collect or FromIterator::from_iter if you know that the iterator is sorted and has no overlapping items.

§Correctness

The caller must ensure that:

  • The iterator yields items in ascending order according to T: Ord
  • There are no overlapping items

If these invariants are not upheld, the set will behave incorrectly (e.g., FrozenSet::contains may fail to find items that are present), but no memory unsafety will occur.

When debug_assertions is enabled, this will panic if an invariant is not upheld.

Source§

impl<T> FrozenSet<T>

Source

pub const fn len(&self) -> usize

Returns the number of elements in the set.

Source

pub const fn is_empty(&self) -> bool

Returns true if the set contains no elements.

Source

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q> + Ord, Q: Ord + ?Sized,

Returns true if the set contains an element equal to the value.

Source

pub fn get<Q>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q> + Ord, Q: Ord + ?Sized,

Returns a reference to the element in the set, if any, that is equal to the value.

Source

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

Returns a reference to the first element in the set, if any. This element is always the minimum of all elements in the set.

Source

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

Returns a reference to the last element in the set, if any. This element is always the maximum of all elements in the set.

Source

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

Gets an iterator that visits the elements in the FrozenSet in ascending order.

Source

pub fn range<Q, R>(&self, range: R) -> Range<'_, T>
where Q: Ord + ?Sized, T: Borrow<Q> + Ord, R: RangeBounds<Q>,

Constructs a double-ended iterator over a sub-range of elements in the set.

Source

pub fn is_disjoint(&self, other: &Self) -> bool
where T: Ord,

Returns true if self has no elements in common with other. This is equivalent to checking for an empty intersection.

Source

pub fn is_subset(&self, other: &Self) -> bool
where T: Ord,

Returns true if the set is a subset of another, i.e., other contains at least all the elements in self.

Source

pub fn is_superset(&self, other: &Self) -> bool
where T: Ord,

Returns true if the set is a superset of another, i.e., self contains at least all the elements in other.

Trait Implementations§

Source§

impl<'__de, T, __Context> BorrowDecode<'__de, __Context> for FrozenSet<T>
where T: BorrowDecode<'__de, __Context> + '__de,

Source§

fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>

Attempt to decode this type with the given BorrowDecode.
Source§

impl<T: Clone> Clone for FrozenSet<T>

Source§

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

Returns a duplicate 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> Debug for FrozenSet<T>

Source§

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

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

impl<T, __Context> Decode<__Context> for FrozenSet<T>
where T: Decode<__Context> + 'static,

Source§

fn decode<__D: Decoder<Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>

Attempt to decode this type with the given Decode.
Source§

impl<T> Default for FrozenSet<T>

Source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for FrozenSet<T>
where T: Deserialize<'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> Encode for FrozenSet<T>
where T: Encode,

Source§

fn encode<__E: Encoder>(&self, encoder: &mut __E) -> Result<(), EncodeError>

Encode a given type.
Source§

impl<T: Ord, const N: usize> From<[T; N]> for FrozenSet<T>

Source§

fn from(items: [T; N]) -> Self

Creates a FrozenSet from an array of items. If there are overlapping items, the last copy is kept.

The items are sorted during construction.

Source§

impl<T> From<BTreeSet<T>> for FrozenSet<T>

Source§

fn from(set: BTreeSet<T>) -> Self

Creates a FrozenSet from a BTreeSet.

This is more efficient than From<HashSet<T>> because BTreeSet already iterates in sorted order, so no re-sorting is needed.

Source§

impl<T, S> From<HashSet<T, S>> for FrozenSet<T>
where T: Ord, S: BuildHasher,

Source§

fn from(set: HashSet<T, S>) -> Self

Creates a FrozenSet from a HashSet.

The items are sorted during construction.

Source§

impl<T, S> From<IndexSet<T, S>> for FrozenSet<T>
where T: Ord, S: BuildHasher,

Source§

fn from(set: IndexSet<T, S>) -> Self

Creates a FrozenSet from an [IndexSet].

The items are sorted during construction.

Source§

impl<T: Ord> FromIterator<T> for FrozenSet<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(items: I) -> Self

Creates a FrozenSet from an iterator of items. If there are overlapping items, only the last copy is kept.

Source§

impl<T: Hash> Hash for FrozenSet<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T> IntoIterator for &'a FrozenSet<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Keys<'a, T, ()>

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

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for FrozenSet<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoKeys<T, ()>

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

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for FrozenSet<T>

Source§

fn cmp(&self, other: &FrozenSet<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for FrozenSet<T>

Source§

fn eq(&self, other: &FrozenSet<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: PartialOrd> PartialOrd for FrozenSet<T>

Source§

fn partial_cmp(&self, other: &FrozenSet<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Serialize for FrozenSet<T>
where T: Serialize,

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> Eq for FrozenSet<T>

Source§

impl<T> StructuralPartialEq for FrozenSet<T>

Auto Trait Implementations§

§

impl<T> Freeze for FrozenSet<T>

§

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

§

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

§

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

§

impl<T> Unpin for FrozenSet<T>

§

impl<T> UnwindSafe for FrozenSet<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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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