#[non_exhaustive]pub enum TypeName {
Show 13 variants
Primitive(PrimitiveType),
Named(PathType),
Reference(Lifetime, Mutability, Box<TypeName>),
Box(Box<TypeName>),
Option(Box<TypeName>),
Result(Box<TypeName>, Box<TypeName>, bool),
Writeable,
StrReference(Option<Lifetime>, StringEncoding),
PrimitiveSlice(Option<(Lifetime, Mutability)>, PrimitiveType),
StrSlice(StringEncoding),
Unit,
SelfType(PathType),
Ordering,
}
Expand description
A local type reference, such as the type of a field, parameter, or return value.
Unlike CustomType
, which represents a type declaration, TypeName
s can compose
types through references and boxing, and can also capture unresolved paths.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Primitive(PrimitiveType)
A built-in Rust scalar primitive.
Named(PathType)
An unresolved path to a custom type, which can be resolved after all types
are collected with [TypeName::resolve()
].
Reference(Lifetime, Mutability, Box<TypeName>)
An optionally mutable reference to another type.
Box(Box<TypeName>)
A Box<T>
type.
Option(Box<TypeName>)
A Option<T>
type.
Result(Box<TypeName>, Box<TypeName>, bool)
A Result<T, E>
or diplomat_runtime::DiplomatWriteable
type. If the bool is true, it’s Result
Writeable
StrReference(Option<Lifetime>, StringEncoding)
A &DiplomatStr
or Box<DiplomatStr>
type.
Owned strings don’t have a lifetime.
PrimitiveSlice(Option<(Lifetime, Mutability)>, PrimitiveType)
A &[T]
or Box<[T]>
type, where T
is a primitive.
Owned slices don’t have a lifetime or mutability.
StrSlice(StringEncoding)
&[&DiplomatStr]
Unit
The ()
type.
SelfType(PathType)
The Self
type.
Ordering
std::cmp::Ordering or core::cmp::Ordering
The path must be present! Ordering will be parsed as an AST type!
Implementations§
source§impl TypeName
impl TypeName
sourcepub fn to_syn(&self) -> Type
pub fn to_syn(&self) -> Type
Converts the TypeName
back into an AST node that can be spliced into a program.
sourcepub fn from_syn(ty: &Type, self_path_type: Option<PathType>) -> TypeName
pub fn from_syn(ty: &Type, self_path_type: Option<PathType>) -> TypeName
Extract a TypeName
from a syn::Type
AST node.
The following rules are used to infer TypeName
variants:
- If the type is a path with a single element that is the name of a Rust primitive, returns a
TypeName::Primitive
- If the type is a path with a single element
Box
, returns aTypeName::Box
with the type parameter recursively converted - If the type is a path with a single element
Option
, returns aTypeName::Option
with the type parameter recursively converted - If the type is a path with a single element
Self
andself_path_type
is provided, returns aTypeName::Named
- If the type is a path with a single element
Result
, returns aTypeName::Result
with the type parameters recursively converted - If the type is a path equal to [
diplomat_runtime::DiplomatResult
], returns a [TypeName::DiplomatResult
] with the type parameters recursively converted - If the type is a path equal to [
diplomat_runtime::DiplomatWriteable
], returns aTypeName::Writeable
- If the type is a owned or borrowed string type, returns a
TypeName::StrReference
- If the type is a owned or borrowed slice of a Rust primitive, returns a
TypeName::PrimitiveSlice
- If the type is a reference (
&
or&mut
), returns aTypeName::Reference
with the referenced type recursively converted - Otherwise, assume that the reference is to a
CustomType
in either the current module or another one, returns aTypeName::Named
sourcepub fn is_self(&self) -> bool
pub fn is_self(&self) -> bool
Returns true
if self
is the TypeName::SelfType
variant, otherwise
false
.
sourcepub fn visit_lifetimes<'a, F, B>(&'a self, visit: &mut F) -> ControlFlow<B>
pub fn visit_lifetimes<'a, F, B>(&'a self, visit: &mut F) -> ControlFlow<B>
Recurse down the type tree, visiting all lifetimes.
Using this function, you can collect all the lifetimes into a collection, or examine each one without having to make any additional allocations.
sourcepub fn any_lifetime<'a, F>(&'a self, f: F) -> bool
pub fn any_lifetime<'a, F>(&'a self, f: F) -> bool
Returns true
if any lifetime satisfies a predicate, otherwise false
.
This method is short-circuiting, meaning that if the predicate ever succeeds, it will return immediately.
sourcepub fn all_lifetimes<'a, F>(&'a self, f: F) -> bool
pub fn all_lifetimes<'a, F>(&'a self, f: F) -> bool
Returns true
if all lifetimes satisfy a predicate, otherwise false
.
This method is short-circuiting, meaning that if the predicate ever fails, it will return immediately.
sourcepub fn longer_lifetimes<'env>(
&self,
lifetime_env: &'env LifetimeEnv,
) -> Vec<&'env NamedLifetime>
pub fn longer_lifetimes<'env>( &self, lifetime_env: &'env LifetimeEnv, ) -> Vec<&'env NamedLifetime>
Returns all lifetimes in a LifetimeEnv
that must live at least as
long as the type.
sourcepub fn shorter_lifetimes<'env>(
&self,
lifetime_env: &'env LifetimeEnv,
) -> Vec<&'env NamedLifetime>
pub fn shorter_lifetimes<'env>( &self, lifetime_env: &'env LifetimeEnv, ) -> Vec<&'env NamedLifetime>
Returns all lifetimes in a LifetimeEnv
that are outlived by the type.
sourcefn transitive_lifetime_bounds<'env>(
&self,
transitivity: LifetimeTransitivity<'env>,
) -> Vec<&'env NamedLifetime>
fn transitive_lifetime_bounds<'env>( &self, transitivity: LifetimeTransitivity<'env>, ) -> Vec<&'env NamedLifetime>
Visits the provided LifetimeTransitivity
value with all NamedLifetime
s
in the type tree, and returns the transitively reachable lifetimes.
pub fn is_zst(&self) -> bool
pub fn is_pointer(&self) -> bool
Trait Implementations§
source§impl<'de> Deserialize<'de> for TypeName
impl<'de> Deserialize<'de> for TypeName
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for TypeName
impl StructuralPartialEq for TypeName
Auto Trait Implementations§
impl Freeze for TypeName
impl RefUnwindSafe for TypeName
impl Send for TypeName
impl Sync for TypeName
impl Unpin for TypeName
impl UnwindSafe for TypeName
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)