diplomat_core::ast::types

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

fn transitive_lifetime_bounds<'env>( &self, transitivity: LifetimeTransitivity<'env>, ) -> Vec<&'env NamedLifetime>

Visits the provided LifetimeTransitivity value with all NamedLifetimes in the type tree, and returns the transitively reachable lifetimes.

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 copy 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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

default 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>,