Struct parsel::ast::Punctuated

source ·
pub struct Punctuated<T, P> { /* private fields */ }
Expand description

Parses a given production repeatedly, separated by punctuation. Optional trailing punctuation is allowed, and the entire token stream must consist of interleaved representations of T and P. (Thus, this is generally only useful within a delimited group.)

let items: Punctuated<Lit, Token![,]> = parse_quote!(44, true, "str liter", 5.55,);
let items: Vec<Lit> = items.into_iter().collect();

assert_eq!(items, [
    Lit::from(44_u64),
    Lit::from(true),
    Lit::from("str liter"),
    Lit::try_from(5.55).unwrap(),
]);

Implementations§

source§

impl<T, P> Punctuated<T, P>

source

pub const fn new() -> Self

source

pub fn is_empty(&self) -> bool

source

pub fn len(&self) -> usize

source

pub fn into_inner(self) -> Punctuated<T, P>

source

pub fn iter(&self) -> Iter<'_, T>

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

source

pub fn into_pairs(self) -> IntoPairs<T, P>

source

pub fn pairs(&self) -> Pairs<'_, T, P>

source

pub fn pairs_mut(&mut self) -> PairsMut<'_, T, P>

Methods from Deref<Target = Punctuated<T, P>>§

source

pub fn is_empty(&self) -> bool

Determines whether this punctuated sequence is empty, meaning it contains no syntax tree nodes or punctuation.

source

pub fn len(&self) -> usize

Returns the number of syntax tree nodes in this punctuated sequence.

This is the number of nodes of type T, not counting the punctuation of type P.

source

pub fn first(&self) -> Option<&T>

Borrows the first element in this sequence.

source

pub fn first_mut(&mut self) -> Option<&mut T>

Mutably borrows the first element in this sequence.

source

pub fn last(&self) -> Option<&T>

Borrows the last element in this sequence.

source

pub fn last_mut(&mut self) -> Option<&mut T>

Mutably borrows the last element in this sequence.

source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over borrowed syntax tree nodes of type &T.

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns an iterator over mutably borrowed syntax tree nodes of type &mut T.

source

pub fn pairs(&self) -> Pairs<'_, T, P>

Returns an iterator over the contents of this sequence as borrowed punctuated pairs.

source

pub fn pairs_mut(&mut self) -> PairsMut<'_, T, P>

Returns an iterator over the contents of this sequence as mutably borrowed punctuated pairs.

source

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

Appends a syntax tree node onto the end of this punctuated sequence. The sequence must already have a trailing punctuation, or be empty.

Use push instead if the punctuated sequence may or may not already have trailing punctuation.

Panics

Panics if the sequence is nonempty and does not already have a trailing punctuation.

source

pub fn push_punct(&mut self, punctuation: P)

Appends a trailing punctuation onto the end of this punctuated sequence. The sequence must be non-empty and must not already have trailing punctuation.

Panics

Panics if the sequence is empty or already has a trailing punctuation.

source

pub fn pop(&mut self) -> Option<Pair<T, P>>

Removes the last punctuated pair from this sequence, or None if the sequence is empty.

source

pub fn pop_punct(&mut self) -> Option<P>

Removes the trailing punctuation from this punctuated sequence, or None if there isn’t any.

source

pub fn trailing_punct(&self) -> bool

Determines whether this punctuated sequence ends with a trailing punctuation.

source

pub fn empty_or_trailing(&self) -> bool

Returns true if either this Punctuated is empty, or it has a trailing punctuation.

Equivalent to punctuated.is_empty() || punctuated.trailing_punct().

source

pub fn push(&mut self, value: T)where P: Default,

Appends a syntax tree node onto the end of this punctuated sequence.

If there is not a trailing punctuation in this sequence when this method is called, the default value of punctuation type P is inserted before the given value of type T.

source

pub fn insert(&mut self, index: usize, value: T)where P: Default,

Inserts an element at position index.

Panics

Panics if index is greater than the number of elements previously in this punctuated sequence.

source

pub fn clear(&mut self)

Clears the sequence of all values and punctuation, making it empty.

Trait Implementations§

source§

impl<T, P> AsMut<Punctuated<T, P>> for Punctuated<T, P>

source§

fn as_mut(&mut self) -> &mut Punctuated<T, P>

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T, P> AsRef<Punctuated<T, P>> for Punctuated<T, P>

source§

fn as_ref(&self) -> &Punctuated<T, P>

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

impl<T, P> Borrow<Punctuated<T, P>> for Punctuated<T, P>

source§

fn borrow(&self) -> &Punctuated<T, P>

Immutably borrows from an owned value. Read more
source§

impl<T, P> BorrowMut<Punctuated<T, P>> for Punctuated<T, P>

source§

fn borrow_mut(&mut self) -> &mut Punctuated<T, P>

Mutably borrows from an owned value. Read more
source§

impl<T: Clone, P: Clone> Clone for Punctuated<T, P>

source§

fn clone(&self) -> Punctuated<T, P>

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<T, P> Debug for Punctuated<T, P>where T: Debug, P: Debug,

source§

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

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

impl<T, P> Default for Punctuated<T, P>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T, P> Deref for Punctuated<T, P>

§

type Target = Punctuated<T, P>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T, P> DerefMut for Punctuated<T, P>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T, P> Display for Punctuated<T, P>where T: ToTokens, P: ToTokens,

source§

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

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

impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P>where P: Default,

source§

fn extend<I>(&mut self, iter: I)where I: IntoIterator<Item = Pair<T, P>>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T, P> Extend<T> for Punctuated<T, P>where P: Default,

source§

fn extend<I>(&mut self, iter: I)where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T, P> From<Punctuated<T, P>> for Punctuated<T, P>

source§

fn from(punctuated: Punctuated<T, P>) -> Self

Converts to this type from the input type.
source§

impl<T, P> From<Punctuated<T, P>> for Punctuated<T, P>

source§

fn from(inner: Punctuated<T, P>) -> Self

Converts to this type from the input type.
source§

impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P>

source§

fn from_iter<I>(iter: I) -> Selfwhere I: IntoIterator<Item = Pair<T, P>>,

Creates a value from an iterator. Read more
source§

impl<T, P> FromIterator<T> for Punctuated<T, P>where P: Default,

source§

fn from_iter<I>(iter: I) -> Selfwhere I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
source§

impl<T, P> FromStr for Punctuated<T, P>where T: Parse, P: Parse,

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(string: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl<T: Hash, P: Hash> Hash for Punctuated<T, P>

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<T, P> Index<usize> for Punctuated<T, P>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T, P> IndexMut<usize> for Punctuated<T, P>

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a, T, P> IntoIterator for &'a Punctuated<T, P>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, P> IntoIterator for Punctuated<T, P>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, P> Parse for Punctuated<T, P>where T: Parse, P: Parse,

source§

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

source§

impl<T: PartialEq, P: PartialEq> PartialEq<Punctuated<T, P>> for Punctuated<T, P>

source§

fn eq(&self, other: &Punctuated<T, P>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, P> ToTokens for Punctuated<T, P>where T: ToTokens, P: ToTokens,

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) -> TokenStreamwhere Self: Sized,

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

impl<T: Eq, P: Eq> Eq for Punctuated<T, P>

source§

impl<T, P> StructuralEq for Punctuated<T, P>

source§

impl<T, P> StructuralPartialEq for Punctuated<T, P>

Auto Trait Implementations§

§

impl<T, P> RefUnwindSafe for Punctuated<T, P>where P: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, P> Send for Punctuated<T, P>where P: Send, T: Send,

§

impl<T, P> Sync for Punctuated<T, P>where P: Sync, T: Sync,

§

impl<T, P> Unpin for Punctuated<T, P>where P: Unpin, T: Unpin,

§

impl<T, P> UnwindSafe for Punctuated<T, P>where P: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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> Spanned for Twhere 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> SpannedExt for Twhere T: Spanned + ?Sized,

source§

fn byte_range(&self, source: &str) -> Range<usize>

TODO(H2CO3): a faster, less naive implementation would be great. We should use the byte offset of start to compute that of end, sparing the double scan of the source up until the start location.

let source = r#"
   -3.667
  1248  "string ű literal"
      "wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;

assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].byte_range(source),  4..10);
assert_eq!(tokens[1].byte_range(source), 13..17);
assert_eq!(tokens[2].byte_range(source), 19..38);
assert_eq!(tokens[3].byte_range(source), 45..54);
source§

fn char_range(&self, source: &str) -> Range<usize>

TODO(H2CO3): a faster, less naive implementation would be great. We should use the char offset of start to compute that of end, sparing the double scan of the source up until the start location.

let source = r#"
   -3.667
  1248  "string ű literal"
      "wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;

assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].char_range(source),  4..10);
assert_eq!(tokens[1].char_range(source), 13..17);
assert_eq!(tokens[2].char_range(source), 19..37);
assert_eq!(tokens[3].char_range(source), 44..51);
source§

fn format_span(&self) -> SpanDisplay

source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.