Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement trivial Copy and ToOwned functions for Error<_> #1788

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,48 @@ impl<I: fmt::Display> fmt::Display for Error<I> {
}
}

#[cfg(feature = "alloc")]
impl<I: ToOwned + ?Sized> Error<&I> {
/// Converts `Error<&I>` into `Error<I::Owned>` by cloning.
pub fn cloned(self) -> Error<I::Owned> {
Error {
input: self.input.to_owned(),
code: self.code,
}
}
}

#[cfg(feature = "alloc")]
impl<I: ToOwned + ?Sized> Error<&mut I> {
/// Converts `Error<&mut I>` into `Error<I::Owned>` by cloning.
pub fn cloned(self) -> Error<I::Owned> {
Error {
input: self.input.to_owned(),
code: self.code,
}
}
}

impl<I: Copy> Error<&I> {
/// Converts `Error<&I>` into `Error<I>` by copying.
pub fn copied(self) -> Error<I> {
Error {
input: *self.input,
code: self.code,
}
}
}

impl<I: Copy> Error<&mut I> {
/// Converts `Error<&mut I>` into `Error<I>` by copying.
pub fn copied(self) -> Error<I> {
Error {
input: *self.input,
code: self.code,
}
}
}

#[cfg(feature = "std")]
impl<I: fmt::Debug + fmt::Display> std::error::Error for Error<I> {}

Expand Down Expand Up @@ -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<String> = err.cloned();
}

#[test]
fn copy_error() {
let err = Error {
code: ErrorKind::Eof,
input: &0_u8,
};

let _err: Error<u8> = err.copied();
}
}

/*
Expand Down