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
impl GenericsWithWhere
Sourcepub fn unwrap(self) -> Generics
pub fn unwrap(self) -> Generics
Unwraps the GenericsWithWhere
to retrieve the inner syn ::Generics
.
Sourcepub fn parse_from_str(s: &str) -> Result<GenericsWithWhere>
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 optionalwhere
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
impl Debug for GenericsWithWhere
Source§impl From<Generics> for GenericsWithWhere
impl From<Generics> for GenericsWithWhere
Source§impl From<GenericsWithWhere> for Generics
impl From<GenericsWithWhere> for Generics
Source§fn from(g: GenericsWithWhere) -> Self
fn from(g: GenericsWithWhere) -> Self
Source§impl Parse for GenericsWithWhere
impl Parse for GenericsWithWhere
fn parse(input: ParseStream<'_>) -> Result<Self>
Source§impl ToTokens for GenericsWithWhere
impl ToTokens for GenericsWithWhere
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Source§fn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
Source§fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
Auto Trait Implementations§
impl Freeze for GenericsWithWhere
impl RefUnwindSafe for GenericsWithWhere
impl !Send for GenericsWithWhere
impl !Sync for GenericsWithWhere
impl Unpin for GenericsWithWhere
impl UnwindSafe for GenericsWithWhere
Blanket Implementations§
Source§impl<S> AssignWithType for S
impl<S> AssignWithType for S
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> 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 moreSource§impl<T> Spanned for Twhere
T: Spanned + ?Sized,
impl<T> Spanned for Twhere
T: Spanned + ?Sized,
Source§fn span(&self) -> Span
fn span(&self) -> Span
Span
covering the complete contents of this syntax tree
node, or Span::call_site()
if this node is empty.