Skip to content

Commit

Permalink
Add into_fragment() and into_fragment_and_extra()
Browse files Browse the repository at this point in the history
  • Loading branch information
Palladinium authored and progval committed Aug 22, 2023
1 parent 6a87c69 commit 9aaf7b0
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,42 @@ impl<T, X> LocatedSpan<T, X> {
extra: f(self.extra),
}
}

/// Takes ownership of the fragment without (re)borrowing it.
///
/// # Example of use
/// ```
/// # extern crate nom_locate;
/// # extern crate nom;
/// # use nom_locate::LocatedSpan;
/// use nom::{
/// IResult,
/// bytes::complete::{take_till, tag},
/// combinator::rest,
/// };
///
/// fn parse_pair<'a>(input: LocatedSpan<&'a str>) -> IResult<LocatedSpan<&'a str>, (&'a str, &'a str)> {
/// let (input, key) = take_till(|c| c == '=')(input)?;
/// let (input, _) = tag("=")(input)?;
/// let (input, value) = rest(input)?;
///
/// Ok((input, (key.into_fragment(), value.into_fragment())))
/// }
///
/// fn main() {
/// let span = LocatedSpan::new("key=value");
/// let (_, pair) = parse_pair(span).unwrap();
/// assert_eq!(pair, ("key", "value"));
/// }
/// ```
pub fn into_fragment(self) -> T {
self.fragment
}

/// Takes ownership of the fragment and extra data without (re)borrowing them.
pub fn into_fragment_and_extra(self) -> (T, X) {
(self.fragment, self.extra)
}
}

impl<T: AsBytes, X> LocatedSpan<T, X> {
Expand Down

0 comments on commit 9aaf7b0

Please sign in to comment.