TypeName

Enum TypeName 

Source
#[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, TypeNames can compose types through references and boxing, and can also capture unresolved paths.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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

Source

pub fn to_syn(&self) -> Type

Converts the TypeName back into an AST node that can be spliced into a program.

Source

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 a TypeName::Box with the type parameter recursively converted
  • If the type is a path with a single element Option, returns a TypeName::Option with the type parameter recursively converted
  • If the type is a path with a single element Self and self_path_type is provided, returns a TypeName::Named
  • If the type is a path with a single element Result, returns a TypeName::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 a TypeName::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 a TypeName::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 a TypeName::Named
Source

pub fn is_self(&self) -> bool

Returns true if self is the TypeName::SelfType variant, otherwise false.

Source

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.

Source

pub fn any_lifetime<'a, F>(&'a self, f: F) -> bool
where F: FnMut(&'a Lifetime, LifetimeOrigin) -> 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.

Source

pub fn all_lifetimes<'a, F>(&'a self, f: F) -> bool
where F: FnMut(&'a Lifetime, LifetimeOrigin) -> 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.

Source

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.

Source

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.

Source

pub fn is_zst(&self) -> bool

Source

pub fn is_pointer(&self) -> bool

Trait Implementations§

Source§

impl Clone for TypeName

Source§

fn clone(&self) -> TypeName

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TypeName

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for TypeName

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for TypeName

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for TypeName

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TypeName

Source§

fn eq(&self, other: &TypeName) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for TypeName

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for TypeName

Source§

impl StructuralPartialEq for TypeName

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 56 bytes

Size for each variant:

  • Primitive: 1 byte
  • Named: 55 bytes
  • Reference: 39 bytes
  • Box: 15 bytes
  • Option: 15 bytes
  • Result: 23 bytes
  • Writeable: 0 bytes
  • StrReference: 31 bytes
  • PrimitiveSlice: 39 bytes
  • StrSlice: 1 byte
  • Unit: 0 bytes
  • SelfType: 55 bytes
  • Ordering: 0 bytes