GenericsWithWhere

Struct GenericsWithWhere 

Source
pub struct GenericsWithWhere {
    pub generics: Generics,
}
Expand description

A GenericsWithWhere struct to handle the parsing of Rust generics with an explicit where clause.

This wrapper addresses the limitation in the syn crate where parsing Generics directly from a ParseStream does not automatically handle associated where clauses. By integrating where clause parsing into the GenericsWithWhere, this struct provides a seamless way to capture both the generics and their constraints in scenarios where the where clause is crucial for type constraints and bounds in Rust macros and code generation.

Usage :

let parsed_generics: macro_tools ::generic_params ::GenericsWithWhere
= syn ::parse_str( "< T: Clone, U: Default = Default1 > where T: Default" ).unwrap();
assert!( parsed_generics.generics.params.len() == 2 );
assert!( parsed_generics.generics.where_clause.is_some() );

Fields§

§generics: Generics

Syn’s generics parameters.

Implementations§

Source§

impl GenericsWithWhere

Source

pub fn unwrap(self) -> Generics

Unwraps the GenericsWithWhere to retrieve the inner syn ::Generics.

Source

pub fn parse_from_str(s: &str) -> Result<GenericsWithWhere>

Parses a string to a GenericsWithWhere, specifically designed to handle generics syntax with where clauses effectively.

This function provides a convenient way to parse generic parameters and their associated where clauses from a string slice, returning a GenericsWithWhere instance.

§Arguments
  • s - The string slice containing the generics and optional where clause (e.g., "< T: Debug > where T: Default").
§Returns

Returns a syn ::Result which is Ok(GenericsWithWhere) on successful parsing, or Err(syn ::Error) if the input string does not conform to valid Rust generics syntax.

§Errors

Returns a syn ::Error if the input string s cannot be parsed as valid Rust generics or a where clause.

§Examples
use macro_tools ::generic_params ::GenericsWithWhere;

let parsed = GenericsWithWhere ::parse_from_str( "< T: Clone, U: Default = Default1 > where T: Default" ).unwrap();
assert!( parsed.generics.params.len() == 2 );
assert!( parsed.generics.where_clause.is_some() );

let parsed_no_where = GenericsWithWhere ::parse_from_str( "< T >" ).unwrap();
assert!( parsed_no_where.generics.params.len() == 1 );
assert!( parsed_no_where.generics.where_clause.is_none() );

let parsed_only_where = GenericsWithWhere ::parse_from_str( "where T: Debug" ).unwrap();
assert!( parsed_only_where.generics.params.is_empty() );
assert!( parsed_only_where.generics.where_clause.is_some() );

Trait Implementations§

Source§

impl Debug for GenericsWithWhere

Source§

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

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

impl From<Generics> for GenericsWithWhere

Source§

fn from(generics: Generics) -> Self

Converts to this type from the input type.
Source§

impl From<GenericsWithWhere> for Generics

Source§

fn from(g: GenericsWithWhere) -> Self

Converts to this type from the input type.
Source§

impl Parse for GenericsWithWhere

Source§

fn parse(input: ParseStream<'_>) -> Result<Self>

Source§

impl ToTokens for GenericsWithWhere

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Write self to the given TokenStream. Read more
Source§

fn to_token_stream(&self) -> TokenStream

Convert self directly into a TokenStream object. Read more
Source§

fn into_token_stream(self) -> TokenStream
where Self: Sized,

Convert self directly into a TokenStream object. Read more

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<S> AssignWithType for S

Source§

fn assign_with_type<T, IntoT>(&mut self, component: IntoT)
where IntoT: Into<T>, S: Assign<T, IntoT>,

Sets the value of a component by its type. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Spanned for T
where T: Spanned + ?Sized,

Source§

fn span(&self) -> Span

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty.
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.