pub struct NameGenerator { /* private fields */ }
Expand description
Generator for new names
The generator keeps track of existing names and suggests new names that do not conflict with existing names.
The generator will try to resolve conflicts by adding a numeric suffix to
the name, e.g. a
, a1
, a2
, …
§Examples
let mut generator = NameGenerator::new();
assert_eq!(generator.suggest_name("a"), "a");
assert_eq!(generator.suggest_name("a"), "a1");
assert_eq!(generator.suggest_name("b2"), "b2");
assert_eq!(generator.suggest_name("b"), "b3");
Implementations§
source§impl NameGenerator
impl NameGenerator
sourcepub fn new_with_names<'a>(existing_names: impl Iterator<Item = &'a str>) -> Self
pub fn new_with_names<'a>(existing_names: impl Iterator<Item = &'a str>) -> Self
Create a new generator with existing names. When suggesting a name, it will avoid conflicts with existing names.
sourcepub fn suggest_name(&mut self, name: &str) -> SmolStr
pub fn suggest_name(&mut self, name: &str) -> SmolStr
Suggest a name without conflicts. If the name conflicts with existing names, it will try to resolve the conflict by adding a numeric suffix.
sourcepub fn for_type(
&mut self,
ty: &Type,
db: &RootDatabase,
edition: Edition,
) -> Option<SmolStr>
pub fn for_type( &mut self, ty: &Type, db: &RootDatabase, edition: Edition, ) -> Option<SmolStr>
Suggest a name for given type.
The function will strip references first, and suggest name from the inner type.
- If
ty
is an ADT, it will suggest the name of the ADT.- If
ty
is wrapped inBox
,Option
orResult
, it will suggest the name from the inner type.
- If
- If
ty
is a trait, it will suggest the name of the trait. - If
ty
is animpl Trait
, it will suggest the name of the first trait.
If the suggested name conflicts with reserved keywords, it will return None
.
sourcepub fn for_impl_trait_as_generic(&mut self, ty: &ImplTraitType) -> SmolStr
pub fn for_impl_trait_as_generic(&mut self, ty: &ImplTraitType) -> SmolStr
Suggest name of impl trait type
§Current implementation
In current implementation, the function tries to get the name from the first character of the name for the first type bound.
If the name conflicts with existing generic parameters, it will try to
resolve the conflict with for_unique_generic_name
.
Trait Implementations§
source§impl Debug for NameGenerator
impl Debug for NameGenerator
source§impl Default for NameGenerator
impl Default for NameGenerator
source§fn default() -> NameGenerator
fn default() -> NameGenerator
Auto Trait Implementations§
impl Freeze for NameGenerator
impl RefUnwindSafe for NameGenerator
impl Send for NameGenerator
impl Sync for NameGenerator
impl Unpin for NameGenerator
impl UnwindSafe for NameGenerator
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Cast for T
impl<T> Cast for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more