From e181603f2f700977531cf7dd5ba242fa90f8ed01 Mon Sep 17 00:00:00 2001 From: koshell Date: Sun, 17 Nov 2024 22:41:18 +1100 Subject: [PATCH] Implement trivial Copy and ToOwned functions for Error --- src/error.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/error.rs b/src/error.rs index 93dbd40b..50b31887 100644 --- a/src/error.rs +++ b/src/error.rs @@ -98,6 +98,48 @@ impl fmt::Display for Error { } } +#[cfg(feature = "alloc")] +impl Error<&I> { + /// Converts `Error<&I>` into `Error` by cloning. + pub fn cloned(self) -> Error { + Error { + input: self.input.to_owned(), + code: self.code, + } + } +} + +#[cfg(feature = "alloc")] +impl Error<&mut I> { + /// Converts `Error<&mut I>` into `Error` by cloning. + pub fn cloned(self) -> Error { + Error { + input: self.input.to_owned(), + code: self.code, + } + } +} + +impl Error<&I> { + /// Converts `Error<&I>` into `Error` by copying. + pub fn copied(self) -> Error { + Error { + input: *self.input, + code: self.code, + } + } +} + +impl Error<&mut I> { + /// Converts `Error<&mut I>` into `Error` by copying. + pub fn copied(self) -> Error { + Error { + input: *self.input, + code: self.code, + } + } +} + #[cfg(feature = "std")] impl std::error::Error for Error {} @@ -756,6 +798,27 @@ mod tests { let _result: IResult<_, _, VerboseError<&str>> = char('x')(input); } + + #[cfg(feature = "alloc")] + #[test] + fn clone_error() { + let err = Error { + code: ErrorKind::Eof, + input: "test", + }; + + let _err: Error = err.cloned(); + } + + #[test] + fn copy_error() { + let err = Error { + code: ErrorKind::Eof, + input: &0_u8, + }; + + let _err: Error = err.copied(); + } } /*