pub trait CollectionAssign: Collection + IntoIterator<Item = Self::Entry> {
// Required method
fn assign<Entries>(&mut self, entries: Entries) -> usize
where Entries: IntoIterator<Item = Self::Entry>;
}
Expand description
Defines the capability to replace all entries in a collection with a new set of entries.
This trait extends the Collection
trait by providing a method to replace the existing entries in
the collection with a new set. This can be useful for resetting the collection’s contents or bulk-updating
them based on external criteria or operations.
Required Methods§
Sourcefn assign<Entries>(&mut self, entries: Entries) -> usizewhere
Entries: IntoIterator<Item = Self::Entry>,
fn assign<Entries>(&mut self, entries: Entries) -> usizewhere
Entries: IntoIterator<Item = Self::Entry>,
Replaces all entries in the collection with the provided entries and returns the count of new entries added.
This method clears the existing entries and populates the collection with new ones provided by an iterator. It is ideal for scenarios where the collection needs to be refreshed or updated with a new batch of entries.
§Parameters
entries
: An iterator over the entries to be added to the collection. The entries must conform to theEntry
type defined by theCollection
trait.
§Returns
Returns the number of entries successfully added to the collection. This count may differ from the total number of entries in the iterator if the collection imposes restrictions such as capacity limits or duplicate handling.
§Examples
use former_types::{ Collection, CollectionAssign }; // use crate `former` instead of crate `former_types` unless you need to use crate `former_types` directly
struct MyCollection
{
entries : Vec< i32 >,
}
impl Collection for MyCollection
{
type Entry = i32;
type Val = i32;
#[ inline( always ) ]
fn entry_to_val( e : Self::Entry ) -> Self::Val
{
e
}
}
impl IntoIterator for MyCollection
{
type Item = i32;
// type IntoIter = std::vec::IntoIter< i32 >;
type IntoIter = collection_tools::vec::IntoIter< i32 >;
// qqq : zzz : make sure collection_tools has itearators -- done
fn into_iter( self ) -> Self::IntoIter
{
self.entries.into_iter() // Create an iterator from the internal HashSet.
}
}
impl CollectionAssign for MyCollection
{
fn assign< Entries >( &mut self, entries : Entries ) -> usize
where
Entries : IntoIterator< Item = Self::Entry >,
{
self.entries.clear();
self.entries.extend( entries );
self.entries.len()
}
}
let mut collection = MyCollection { entries : vec![ 1, 2, 3 ] };
let new_elements = vec![ 4, 5, 6 ];
assert_eq!( collection.assign( new_elements ), 3 ); // Collection now contains [ 4, 5, 6 ]
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.