Trait former::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§
sourcefn add(&mut self, e: Self::Entry) -> bool
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 typeEntry
is defined by theCollection
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