pub struct FixedSet<T, const SIZE: usize> { /* private fields */ }Expand description
A fixed-capacity set for storing unique elements.
FixedSet maintains a collection of unique elements with compile-time capacity bounds.
It provides efficient insertion, removal, and lookup operations while maintaining
element uniqueness.
§Type Parameters
T- The element type. Must implementDefault + Copy + PartialEq.SIZE- The maximum number of elements the set can hold (compile-time constant).
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 8> = FixedSet::new();
set.insert(3).unwrap();
set.insert(5).unwrap();
assert!(set.contains(&3));
assert_eq!(set.len(), 2);
// Duplicate insertion fails
assert!(set.insert(3).is_err());
// Remove elements
assert_eq!(set.remove(&3), Some(3));
assert_eq!(set.len(), 1);§Memory Layout
The set stores elements in a contiguous array followed by a length field. Element order is not preserved across insertions and removals due to the swap-remove optimization used for O(1) removal.
§Bytemuck Compatibility
FixedSet implements Pod and Zeroable when T implements these traits,
making it suitable for zero-copy serialization in on-chain programs.
Implementations§
Source§impl<T, const SIZE: usize> FixedSet<T, SIZE>
impl<T, const SIZE: usize> FixedSet<T, SIZE>
Sourcepub fn new() -> FixedSet<T, SIZE>
pub fn new() -> FixedSet<T, SIZE>
Creates an empty FixedSet.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let set: FixedSet<u32, 10> = FixedSet::new();
assert!(set.is_empty());
assert_eq!(set.len(), 0);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements currently stored in the set.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
assert_eq!(set.len(), 0);
set.insert(42).unwrap();
assert_eq!(set.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
assert!(set.is_empty());
set.insert(42).unwrap();
assert!(!set.is_empty());Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Returns an iterator over all items currently in the set.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
set.insert(1).unwrap();
set.insert(2).unwrap();
let collected: Vec<_> = set.iter().copied().collect();
assert_eq!(collected.len(), 2);
assert!(collected.contains(&1));
assert!(collected.contains(&2));Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Returns a mutable iterator over all items currently in the set.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
set.insert(1).unwrap();
set.insert(2).unwrap();
for item in set.iter_mut() {
*item *= 10;
}
assert!(set.contains(&10));
assert!(set.contains(&20));Sourcepub fn insert_or_modify<E, F>(&mut self, item: T, modify: F) -> Result<(), E>
pub fn insert_or_modify<E, F>(&mut self, item: T, modify: F) -> Result<(), E>
Inserts item into the set if it is not already present.
If it is present, modifies it using the provided function.
This is useful for implementing “upsert” semantics where you want to insert a new item or update an existing one.
§Examples
use satellite_bitcoin::generic::fixed_set::{FixedSet, FixedSetError};
#[derive(Clone, Copy, Debug, Default)]
struct Counter { id: u32, count: u32 }
impl PartialEq for Counter {
fn eq(&self, other: &Self) -> bool {
self.id == other.id // Only compare by ID
}
}
impl Eq for Counter {}
#[derive(Debug)]
enum MyError {
SetError(FixedSetError),
}
impl From<FixedSetError> for MyError {
fn from(e: FixedSetError) -> Self {
MyError::SetError(e)
}
}
let mut set: FixedSet<Counter, 10> = FixedSet::new();
// Insert new item
set.insert_or_modify(
Counter { id: 1, count: 1 },
|_| Ok::<(), MyError>(())
).unwrap();
// Update existing item
set.insert_or_modify(
Counter { id: 1, count: 0 }, // count doesn't matter for lookup
|existing| { existing.count += 1; Ok::<(), MyError>(()) }
).unwrap();
assert_eq!(set.find(&Counter { id: 1, count: 0 }).unwrap().count, 2);Sourcepub fn contains<Q>(&self, item: &Q) -> boolwhere
T: PartialEq<Q>,
pub fn contains<Q>(&self, item: &Q) -> boolwhere
T: PartialEq<Q>,
Returns true if the set already contains item.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
assert!(!set.contains(&42));
set.insert(42).unwrap();
assert!(set.contains(&42));Sourcepub fn find<Q>(&self, item: &Q) -> Option<&T>where
T: PartialEq<Q>,
pub fn find<Q>(&self, item: &Q) -> Option<&T>where
T: PartialEq<Q>,
Returns the first item equal to item in the set.
This is useful when you need to access the actual stored item that matches your query, especially when using custom equality implementations.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
set.insert(42).unwrap();
assert_eq!(set.find(&42), Some(&42));
assert_eq!(set.find(&99), None);Sourcepub fn find_mut<Q>(&mut self, item: &Q) -> Option<&mut T>where
T: PartialEq<Q>,
pub fn find_mut<Q>(&mut self, item: &Q) -> Option<&mut T>where
T: PartialEq<Q>,
Returns the first item equal to item in the set (mutable reference).
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
set.insert(42).unwrap();
if let Some(item) = set.find_mut(&42) {
*item = 100;
}
assert!(set.contains(&100));
assert!(!set.contains(&42));Sourcepub fn insert(&mut self, item: T) -> Result<(), FixedSetError>
pub fn insert(&mut self, item: T) -> Result<(), FixedSetError>
Attempts to insert item into the set.
§Errors
- Returns
Err(FixedSetError::Duplicate)if the item already exists in the set. - Returns
Err(FixedSetError::Full)if the set is at full capacity.
§Examples
use satellite_bitcoin::generic::fixed_set::{FixedSet, FixedSetError};
let mut set: FixedSet<u32, 2> = FixedSet::new();
assert!(set.insert(1).is_ok());
assert!(set.insert(2).is_ok());
// Duplicate
assert_eq!(set.insert(1), Err(FixedSetError::Duplicate));
// Full
assert_eq!(set.insert(3), Err(FixedSetError::Full));Sourcepub fn remove<Q>(&mut self, item: &Q) -> Option<T>where
T: PartialEq<Q>,
pub fn remove<Q>(&mut self, item: &Q) -> Option<T>where
T: PartialEq<Q>,
Removes an item equal to item from the set and returns it.
Uses swap-remove for O(1) performance, which means element order is not preserved.
§Examples
use satellite_bitcoin::generic::fixed_set::FixedSet;
let mut set: FixedSet<u32, 10> = FixedSet::new();
set.insert(42).unwrap();
assert_eq!(set.remove(&42), Some(42));
assert_eq!(set.remove(&42), None);
assert_eq!(set.len(), 0);Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes and returns an arbitrary element from the set (the last one).
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns the elements of the set as a mutable slice.
Sourcepub fn try_from_iter<I>(iter: I) -> Result<FixedSet<T, SIZE>, FixedSetError>where
I: IntoIterator<Item = T>,
pub fn try_from_iter<I>(iter: I) -> Result<FixedSet<T, SIZE>, FixedSetError>where
I: IntoIterator<Item = T>,
Creates a FixedSet from any iterator, returning an error if the
number of unique elements exceeds the set’s capacity.
Duplicate elements yielded by the iterator are ignored (because a set
cannot contain the same value twice). However, if inserting additional
unique elements would exceed the compile-time capacity SIZE, the
construction fails with FixedSetError::Full.
Trait Implementations§
impl<T, const SIZE: usize> Copy for FixedSet<T, SIZE>where
T: Copy,
Source§impl<T, const SIZE: usize> FixedCapacitySet for FixedSet<T, SIZE>
impl<T, const SIZE: usize> FixedCapacitySet for FixedSet<T, SIZE>
Source§fn find<Q>(
&self,
item: &Q,
) -> Option<&<FixedSet<T, SIZE> as FixedCapacitySet>::Item>
fn find<Q>( &self, item: &Q, ) -> Option<&<FixedSet<T, SIZE> as FixedCapacitySet>::Item>
item in the set.Source§fn find_mut<Q>(
&mut self,
item: &Q,
) -> Option<&mut <FixedSet<T, SIZE> as FixedCapacitySet>::Item>
fn find_mut<Q>( &mut self, item: &Q, ) -> Option<&mut <FixedSet<T, SIZE> as FixedCapacitySet>::Item>
item in the set and return it.Source§fn insert(
&mut self,
item: <FixedSet<T, SIZE> as FixedCapacitySet>::Item,
) -> Result<(), FixedSetError>
fn insert( &mut self, item: <FixedSet<T, SIZE> as FixedCapacitySet>::Item, ) -> Result<(), FixedSetError>
Source§fn insert_or_modify<E, F>(
&mut self,
item: <FixedSet<T, SIZE> as FixedCapacitySet>::Item,
modify: F,
) -> Result<(), E>where
F: FnMut(&mut <FixedSet<T, SIZE> as FixedCapacitySet>::Item) -> Result<(), E>,
E: From<FixedSetError>,
fn insert_or_modify<E, F>(
&mut self,
item: <FixedSet<T, SIZE> as FixedCapacitySet>::Item,
modify: F,
) -> Result<(), E>where
F: FnMut(&mut <FixedSet<T, SIZE> as FixedCapacitySet>::Item) -> Result<(), E>,
E: From<FixedSetError>,
item into the set if it is not already present.
If it is present, modifies it using the provided function.
Returns Ok(()) on success or an appropriate FixedSetError.Source§fn remove<Q>(
&mut self,
item: &Q,
) -> Option<<FixedSet<T, SIZE> as FixedCapacitySet>::Item>
fn remove<Q>( &mut self, item: &Q, ) -> Option<<FixedSet<T, SIZE> as FixedCapacitySet>::Item>
item if present, returning it.Source§fn as_slice(&self) -> &[<FixedSet<T, SIZE> as FixedCapacitySet>::Item]
fn as_slice(&self) -> &[<FixedSet<T, SIZE> as FixedCapacitySet>::Item]
Source§fn as_mut_slice(
&mut self,
) -> &mut [<FixedSet<T, SIZE> as FixedCapacitySet>::Item]
fn as_mut_slice( &mut self, ) -> &mut [<FixedSet<T, SIZE> as FixedCapacitySet>::Item]
impl<T, const SIZE: usize> Pod for FixedSet<T, SIZE>where
T: Pod,
Source§impl<T, const SIZE: usize> PushPopCollection<T> for FixedSet<T, SIZE>
impl<T, const SIZE: usize> PushPopCollection<T> for FixedSet<T, SIZE>
Auto Trait Implementations§
impl<T, const SIZE: usize> Freeze for FixedSet<T, SIZE>where
T: Freeze,
impl<T, const SIZE: usize> RefUnwindSafe for FixedSet<T, SIZE>where
T: RefUnwindSafe,
impl<T, const SIZE: usize> Send for FixedSet<T, SIZE>where
T: Send,
impl<T, const SIZE: usize> Sync for FixedSet<T, SIZE>where
T: Sync,
impl<T, const SIZE: usize> Unpin for FixedSet<T, SIZE>where
T: Unpin,
impl<T, const SIZE: usize> UnsafeUnpin for FixedSet<T, SIZE>where
T: UnsafeUnpin,
impl<T, const SIZE: usize> UnwindSafe for FixedSet<T, SIZE>where
T: UnwindSafe,
Blanket Implementations§
impl<T> AnyBitPattern for Twhere
T: Pod,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
type Bits = T
Self must have the same layout as the specified Bits except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern.Source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self.