From 9aaf7b01d1c4d4662f7310f3d18e9eb7488a3ee2 Mon Sep 17 00:00:00 2001 From: Patrick Chieppe Date: Tue, 22 Aug 2023 21:03:56 +1000 Subject: [PATCH] Add into_fragment() and into_fragment_and_extra() --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 66a19f4..d6fc6d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -321,6 +321,42 @@ impl LocatedSpan { 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, (&'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 LocatedSpan {