pub struct NonEmptyOption<T>(pub Option<T>);Expand description
NonEmptyOption<T> prevents Option from matching when T can succeed with empty
input. It ensures None is returned when no tokens remain, regardless of whether T
could succeed on an empty stream. This is crucial when parsing optional trailing content
that should only match if tokens are actually available to consume.
§Example
let mut token_iter = "ident".to_token_iter();
let parsed = NonEmptyOption::<Punct>::parser(&mut token_iter).unwrap();
assert_tokens_eq!(parsed, "");§Contrast with Option<T>
// Vec<T> can succeed with zero items
let mut token_iter = "".to_token_iter();
// Option<T> returns Some(empty_vec) even at EndOfStream
let parsed = Option::<Vec<Ident>>::parser(&mut token_iter).unwrap();
assert!(parsed.is_some());
// NonEmptyOption<T> returns None at EndOfStream
let mut token_iter = "".to_token_iter();
let parsed = NonEmptyOption::<Vec<Ident>>::parser(&mut token_iter).unwrap();
assert!(parsed.is_none());Tuple Fields§
§0: Option<T>Methods from Deref<Target = Option<T>>§
1.0.0 · Sourcepub fn as_ref(&self) -> Option<&T>
pub fn as_ref(&self) -> Option<&T>
Converts from &Option<T> to Option<&T>.
§Examples
Calculates the length of an Option<String> as an Option<usize>
without moving the String. The map method takes the self argument by value,
consuming the original, so this technique uses as_ref to first take an Option to a
reference to the value inside the original.
let text: Option<String> = Some("Hello, world!".to_string());
// First, cast `Option<String>` to `Option<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `text` on the stack.
let text_length: Option<usize> = text.as_ref().map(|s| s.len());
println!("still can print text: {text:?}");1.75.0 · Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice of the contained value, if any. If this is None, an
empty slice is returned. This can be useful to have a single type of
iterator over an Option or slice.
Note: Should you have an Option<&T> and wish to get a slice of T,
you can unpack it via opt.map_or(&[], std::slice::from_ref).
§Examples
assert_eq!(
[Some(1234).as_slice(), None.as_slice()],
[&[1234][..], &[][..]],
);The inverse of this function is (discounting
borrowing) [_]::first:
for i in [Some(1234_u16), None] {
assert_eq!(i.as_ref(), i.as_slice().first());
}1.40.0 · Sourcepub fn as_deref(&self) -> Option<&<T as Deref>::Target>where
T: Deref,
pub fn as_deref(&self) -> Option<&<T as Deref>::Target>where
T: Deref,
Converts from Option<T> (or &Option<T>) to Option<&T::Target>.
Leaves the original Option in-place, creating a new one with a reference
to the original one, additionally coercing the contents via Deref.
§Examples
let x: Option<String> = Some("hey".to_owned());
assert_eq!(x.as_deref(), Some("hey"));
let x: Option<String> = None;
assert_eq!(x.as_deref(), None);Trait Implementations§
Source§impl<T> Clone for NonEmptyOption<T>where
T: Clone,
impl<T> Clone for NonEmptyOption<T>where
T: Clone,
Source§fn clone(&self) -> NonEmptyOption<T>
fn clone(&self) -> NonEmptyOption<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for NonEmptyOption<T>where
T: Debug,
impl<T> Debug for NonEmptyOption<T>where
T: Debug,
Source§impl<T> Deref for NonEmptyOption<T>
impl<T> Deref for NonEmptyOption<T>
Source§impl<T> Parser for NonEmptyOption<T>where
T: Parse,
impl<T> Parser for NonEmptyOption<T>where
T: Parse,
Source§fn parser(tokens: &mut TokenIter) -> Result<NonEmptyOption<T>, Error>
fn parser(tokens: &mut TokenIter) -> Result<NonEmptyOption<T>, Error>
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 moreSource§impl<T> ToTokens for NonEmptyOption<T>where
T: ToTokens,
impl<T> ToTokens for NonEmptyOption<T>where
T: ToTokens,
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Source§fn into_token_iter(self) -> TokenIter ⓘwhere
Self: Sized,
fn into_token_iter(self) -> TokenIter ⓘwhere
Self: Sized,
self into a TokenIter object.Source§fn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
&self into a TokenStream object.Source§fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
self into a TokenStream object.Auto Trait Implementations§
impl<T> Freeze for NonEmptyOption<T>where
T: Freeze,
impl<T> RefUnwindSafe for NonEmptyOption<T>where
T: RefUnwindSafe,
impl<T> Send for NonEmptyOption<T>where
T: Send,
impl<T> Sync for NonEmptyOption<T>where
T: Sync,
impl<T> Unpin for NonEmptyOption<T>where
T: Unpin,
impl<T> UnwindSafe for NonEmptyOption<T>where
T: UnwindSafe,
Blanket Implementations§
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DynamicTokens for T
impl<T> DynamicTokens for T
Source§impl<T> Parse for Twhere
T: Parser,
impl<T> Parse for Twhere
T: Parser,
Source§fn parse(tokens: &mut TokenIter) -> Result<Self, Error>
fn parse(tokens: &mut TokenIter) -> Result<Self, Error>
parser() within a
transaction. Commits changes on success and returns the parsed value. Read more