Struct UniqueNameGenerator

Source
pub struct UniqueNameGenerator {
    pub existing_names: HashSet<String>,
    /* private fields */
}
Expand description

Generates names that are based on a prefix and avoids collisions with names already taken.

Fields§

§existing_names: HashSet<String>

Names that are already taken i.e. would never be generated again.

Implementations§

Source§

impl UniqueNameGenerator

Source

pub fn is_name_conflicting<T: ToString>(&self, name: T) -> bool

Returns true only if name is present in UniqueNameGenerator::existing_names.

Source

pub fn add_name<T: ToString>(&mut self, name: T)

Records name into the set of existing names.

Source

pub fn add_names<I: ToString, T: IntoIterator<Item = I>>(&mut self, names: T)

Calls UniqueNameGenerator::add_name on each name in names.

Source

pub fn get<T: ToString>(&mut self, based_on: T) -> String

Returns a name prefixed with based_on that is not present in [’UniqueNameGenerator::existing_names`].

Examples found in repository?
examples/ex_name_gen.rs (line 5)
3fn main() {
4    let mut vng = make_unique_name_gen([]);
5    println!("vng.get(foo) => {}", vng.get("foo"));
6    println!("vng.get(foo) => {}", vng.get("foo"));
7    println!("vng.get(bar_2) => {}", vng.get("bar_2"));
8    println!("vng.get(bar_2) => {}", vng.get("bar_2"));
9    println!("vng.get(bar_2) => {}", vng.get("bar_2"));
10}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.