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

Merged
merged 3 commits into from
Dec 8, 2024
Merged
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
64 changes: 64 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,48 @@
}
}

#[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 @@ -278,7 +320,7 @@
}
}

use crate::internal::{Err, IResult};

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unused import: `IResult`

Check warning on line 323 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unused import: `IResult`

/// Create a new error from an input position, a static string and an existing error.
/// This is used mainly in the [context] combinator, to add user friendly information
Expand Down Expand Up @@ -691,7 +733,7 @@
#[cfg(test)]
mod tests {
use super::*;
use crate::character::complete::char;

Check warning on line 736 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

unused import: `crate::character::complete::char`

Check warning on line 736 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unused import: `crate::character::complete::char`

#[test]
fn context_test() {
Expand Down Expand Up @@ -756,6 +798,28 @@

let _result: IResult<_, _, VerboseError<&str>> = char('x')(input);
}

#[cfg(feature = "alloc")]
#[test]
fn clone_error() {
use crate::lib::std::string::String;
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
Loading