Trait former::exposed::CollectionAssign

source ·
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§

source

fn assign<Entries>(&mut self, entries: Entries) -> usize
where 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 the Entry type defined by the Collection 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 ]

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<E> CollectionAssign for BinaryHeap<E>
where E: Ord,

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <BinaryHeap<E> as Collection>::Entry>,

source§

impl<E> CollectionAssign for BTreeSet<E>
where E: Ord,

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <BTreeSet<E> as Collection>::Entry>,

source§

impl<E> CollectionAssign for LinkedList<E>

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <LinkedList<E> as Collection>::Entry>,

source§

impl<E> CollectionAssign for VecDeque<E>

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <VecDeque<E> as Collection>::Entry>,

source§

impl<E> CollectionAssign for Vec<E>

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <Vec<E> as Collection>::Entry>,

source§

impl<K> CollectionAssign for HashSet<K>
where K: Eq + Hash,

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <HashSet<K> as Collection>::Entry>,

source§

impl<K, V> CollectionAssign for BTreeMap<K, V>
where K: Ord,

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <BTreeMap<K, V> as Collection>::Entry>,

source§

impl<K, V> CollectionAssign for HashMap<K, V>
where K: Eq + Hash,

source§

fn assign<Elements>(&mut self, elements: Elements) -> usize
where Elements: IntoIterator<Item = <HashMap<K, V> as Collection>::Entry>,

Implementors§