Cached

Struct Cached 

Source
pub struct Cached<T> { /* private fields */ }
Expand description

Getting the underlying string expensive as it always allocates a new String. This type caches the string representation of a given entity. Note that this is only reliable for fundamental entities that represent a single token. Spacing between composed tokens is not stable and should be considered informal only.

§Example

use unsynn::*;
let mut token_iter = "ident 1234".to_token_iter();

let cached_ident = Cached::<Ident>::parse(&mut token_iter).unwrap();
assert!(cached_ident == "ident");

Implementations§

Source§

impl<T> Cached<T>
where T: Parse + ToTokens,

Source

pub fn set(&mut self, value: T)

Sets the value and updates the string representation.

Source§

impl<T> Cached<T>
where T: Parse,

Source

pub fn into_inner(self) -> T

Deconstructs self and returns the inner value.

Source

pub fn into_string(self) -> String

Deconstructs self and returns the contained String representation.

Source

pub fn as_str(&self) -> &str

Gets the cached string representation

Source§

impl<T> Cached<T>
where T: Parse,

Source

pub fn new(s: &str) -> Cached<T>

Creates a new Cached<T> from a &str.

§Panics

Panics when s can’t be parsed.

§Example
use unsynn::*;
let cached_ident = Cached::<Ident>::new("ident");
assert!(cached_ident == "ident");
Source

pub fn from_string(s: String) -> Result<Cached<T>, Error>

Creates a new Cached<T> from a owned String.

§Errors

Returns Err when s can’t be parsed.

§Example
use unsynn::*;
let cached_ident = Cached::<Ident>::from_string("ident".into()).unwrap();
assert!(cached_ident == "ident");

Trait Implementations§

Source§

impl<T> AsRef<T> for Cached<T>
where T: Parse,

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> AsRef<str> for Cached<T>
where T: Parse,

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Clone for Cached<T>
where T: Clone,

Source§

fn clone(&self) -> Cached<T>

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<T> Debug for Cached<T>
where T: Parse + Debug,

Source§

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

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

impl<T> Deref for Cached<T>
where T: Parse,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Cached<T> as Deref>::Target

Dereferences the value.
Source§

impl From<Cached<Group>> for Group

Convert CachedGroup into a Group`.

Source§

fn from(cached: Cached<Group>) -> Group

Converts to this type from the input type.
Source§

impl From<Cached<Ident>> for Ident

Convert CachedIdent into a Ident`.

Source§

fn from(cached: Cached<Ident>) -> Ident

Converts to this type from the input type.
Source§

impl From<Cached<Literal>> for Literal

Convert CachedLiteral into a Literal`.

Source§

fn from(cached: Cached<Literal>) -> Literal

Converts to this type from the input type.
Source§

impl From<Cached<LiteralInteger>> for LiteralInteger

Convert CachedLiteralInteger into a LiteralInteger`.

Source§

fn from(cached: Cached<LiteralInteger>) -> LiteralInteger

Converts to this type from the input type.
Source§

impl From<Cached<LiteralString>> for LiteralString

Convert CachedLiteralString into a LiteralString`.

Source§

fn from(cached: Cached<LiteralString>) -> LiteralString

Converts to this type from the input type.
Source§

impl From<Cached<Punct>> for Punct

Convert CachedPunct into a Punct`.

Source§

fn from(cached: Cached<Punct>) -> Punct

Converts to this type from the input type.
Source§

impl<T> From<Cached<T>> for TokenTree
where T: Into<TokenTree>,

Convert a Cached<T: Into<TokenTree>> object into a TokenTree.

Source§

fn from(cached: Cached<T>) -> TokenTree

Converts to this type from the input type.
Source§

impl<T> Hash for Cached<T>
where T: Parse,

Source§

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

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<T> Parser for Cached<T>
where T: Parse + ToTokens,

Source§

fn parser(tokens: &mut TokenIter) -> Result<Cached<T>, Error>

The actual parsing function that must be implemented. This mutates the tokens iterator directly. It should not be called from user code except for implementing parsers itself and then only when the rules below are followed. Read more
Source§

impl<T> PartialEq<&str> for Cached<T>
where T: Parse,

Source§

fn eq(&self, other: &&str) -> 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<T> PartialEq for Cached<T>
where T: Parse,

Source§

fn eq(&self, other: &Cached<T>) -> 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<T> ToTokens for Cached<T>
where T: Parse + ToTokens,

Source§

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

Write &self to the given TokenStream. Read more
Source§

fn to_token_iter(&self) -> TokenIter

Convert &self into a TokenIter object.
Source§

fn into_token_iter(self) -> TokenIter
where Self: Sized,

Convert self into a TokenIter object.
Source§

fn to_token_stream(&self) -> TokenStream

Convert &self into a TokenStream object.
Source§

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

Convert self into a TokenStream object.
Source§

fn tokens_to_string(&self) -> String

Convert &self into a String object. This is mostly used in the test suite to compare the outputs. When the input is a &str then this parses it and returns a normalized String.
Source§

impl<T> TryFrom<&str> for Cached<T>
where T: Parse,

Available on crate feature proc_macro2 only.
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: &str) -> Result<Cached<T>, Error>

Performs the conversion.
Source§

impl<T> TryFrom<String> for Cached<T>
where T: Parse,

Available on crate feature proc_macro2 only.
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: String) -> Result<Cached<T>, Error>

Performs the conversion.
Source§

impl<T> Eq for Cached<T>
where T: Parse,

Auto Trait Implementations§

§

impl<T> Freeze for Cached<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Cached<T>
where T: RefUnwindSafe,

§

impl<T> Send for Cached<T>
where T: Send,

§

impl<T> Sync for Cached<T>
where T: Sync,

§

impl<T> Unpin for Cached<T>
where T: Unpin,

§

impl<T> UnwindSafe for Cached<T>
where T: UnwindSafe,

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> DynamicTokens for T
where T: Any + ToTokens + Debug,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Upcasts &DynamicTokens to &dyn Any. This allows us to stay backward compatible with older rust. Rust 1.86 implements upcast coercion.
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> Parse for T
where T: Parser,

Source§

fn parse(tokens: &mut TokenIter) -> Result<Self, Error>

This is the user facing API to parse grammatical entities. Calls a parser() within a transaction. Commits changes on success and returns the parsed value. Read more
Source§

fn parse_all(tokens: &mut TokenIter) -> Result<Self, Error>

Exhaustive parsing within a transaction. This is a convenience method that implies a EndOfStream at the end. Thus it will error if parsing is not exhaustive. Read more
Source§

fn parse_with<T>( tokens: &mut TokenIter, f: impl FnOnce(Self, &mut TokenIter) -> Result<T, Error>, ) -> Result<T, Error>

Parse a value in a transaction, pass it to a FnOnce(Self, &mut TokenIter) -> Result<T> closure which creates a new result or returns an Error. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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, 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.