Trait former::protected::exposed::CollectionAdd

source ·
pub trait CollectionAdd: Collection {
    // Required method
    fn add(&mut self, e: Self::Entry) -> bool;
}
Expand description

Provides functionality to add individual entries to a collection.

This trait extends the basic Collection trait by introducing a method to add entries to a collection. It is designed to handle the collection’s specific requirements and rules for adding entries, such as managing duplicates, maintaining order, or handling capacity constraints.

Required Methods§

source

fn add(&mut self, e: Self::Entry) -> bool

Adds an entry to the collection and returns a boolean indicating the success of the operation.

Implementations should ensure that the entry is added according to the rules of the collection, which might involve checking for duplicates, ordering, or capacity limits.

§Parameters
  • e: The entry to be added to the collection, where the type Entry is defined by the Collection trait.
§Returns

Returns true if the entry was successfully added, or false if not added due to reasons such as the entry already existing in the collection or the collection reaching its capacity.

§Examples

Basic usage:


use former_types::{ Collection, CollectionAdd }; // 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 CollectionAdd for MyCollection
{
  fn add( &mut self, e : Self::Entry ) -> bool
  {
    if self.entries.contains( &e )
    {
      false
    }
    else
    {
      self.entries.push( e );
      true
    }
  }
}

let mut collection = MyCollection { entries : vec![] };
assert!( collection.add( 10 ) ); // Returns true, entry added
assert!( !collection.add( 10 ) ); // Returns false, entry already exists

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

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

source§

fn add(&mut self, e: <BinaryHeap<E> as Collection>::Entry) -> bool

source§

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

source§

fn add(&mut self, e: <BTreeSet<E> as Collection>::Entry) -> bool

source§

impl<E> CollectionAdd for LinkedList<E>

source§

fn add(&mut self, e: <LinkedList<E> as Collection>::Entry) -> bool

source§

impl<E> CollectionAdd for VecDeque<E>

source§

fn add(&mut self, e: <VecDeque<E> as Collection>::Entry) -> bool

source§

impl<E> CollectionAdd for Vec<E>

source§

fn add(&mut self, e: <Vec<E> as Collection>::Entry) -> bool

source§

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

source§

fn add(&mut self, e: <HashSet<K> as Collection>::Entry) -> bool

source§

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

source§

fn add(&mut self, _: <BTreeMap<K, V> as Collection>::Entry) -> bool

source§

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

source§

fn add(&mut self, _: <HashMap<K, V> as Collection>::Entry) -> bool

Implementors§