Skip to content

Commit

Permalink
[error] Implement core::error::Error for errors (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf committed Sep 15, 2024
1 parent f8134e2 commit a494218
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
28 changes: 27 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
# These are the names of specific Rust versions detected in
# `build.rs`. Each of these represents the minimum Rust version for
# which a particular feature is supported.
"zerocopy-core-error",
"zerocopy-generic-bounds-in-const-fn",
"zerocopy-target-has-atomics",
"zerocopy-aarch64-simd",
Expand Down Expand Up @@ -87,6 +88,8 @@ jobs:
features: "--all-features"
- toolchain: "stable"
features: "--all-features"
- toolchain: "zerocopy-core-error"
features: "--all-features"
- toolchain: "zerocopy-generic-bounds-in-const-fn"
features: "--all-features"
- toolchain: "zerocopy-target-has-atomics"
Expand All @@ -107,6 +110,8 @@ jobs:
# other than "msrv", "stable", and "nightly". These other versions
# exist to exercise zerocopy behavior which differs by toolchain;
# zerocopy-derive doesn't behave different on these toolchains.
- crate: "zerocopy-derive"
toolchain: "zerocopy-core-error"
- crate: "zerocopy-derive"
toolchain: "zerocopy-generic-bounds-in-const-fn"
- crate: "zerocopy-derive"
Expand Down Expand Up @@ -137,7 +142,28 @@ jobs:
target: "thumbv6m-none-eabi"
- toolchain: "zerocopy-aarch64-simd"
target: "wasm32-wasi"
# Exclude most targets targets from the
# Exclude most targets targets from the `zerocopy-core-error`
# toolchain since the `zerocopy-core-error` feature is unrelated to
# compilation target. This only leaves i686 and x86_64 targets.
- toolchain: "zerocopy-core-error"
target: "arm-unknown-linux-gnueabi"
- toolchain: "zerocopy-core-error"
target: "aarch64-unknown-linux-gnu"
- toolchain: "zerocopy-core-error"
target: "powerpc-unknown-linux-gnu"
- toolchain: "zerocopy-core-error"
target: "powerpc64-unknown-linux-gnu"
- toolchain: "zerocopy-core-error"
target: "riscv64gc-unknown-linux-gnu"
- toolchain: "zerocopy-core-error"
target: "s390x-unknown-linux-gnu"
- toolchain: "zerocopy-core-error"
target: "x86_64-pc-windows-msvc"
- toolchain: "zerocopy-core-error"
target: "thumbv6m-none-eabi"
- toolchain: "zerocopy-core-error"
target: "wasm32-wasi"
# Exclude most targets targets from the
# `zerocopy-generic-bounds-in-const-fn` toolchain since the
# `zerocopy-generic-bounds-in-const-fn` feature is unrelated to
# compilation target. This only leaves i686 and x86_64 targets.
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ exclude = [".*"]
# as high as the specified version. In the emitted `--cfg`, dashes are replaced
# by underscores.

# From 1.81.0, Rust supports the `core::error::Error` trait.
zerocopy-core-error = "1.81.0"

# From 1.61.0, Rust supports generic types with trait bounds in `const fn`.
zerocopy-generic-bounds-in-const-fn = "1.61.0"

Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ use std::{env, fs, process::Command, str};
fn main() {
// Avoid unnecessary re-building.
println!("cargo:rerun-if-changed=build.rs");
// This is necessary because changes to the list of detected Rust toolchain
// versions will affect what `--cfg`s this script emits. Without this,
// changes to that list have no effect on the build without running `cargo
// clean` or similar.
println!("cargo:rerun-if-changed=Cargo.toml");

let version_cfgs = parse_version_cfgs_from_cargo_toml();
let rustc_version = rustc_version();
Expand Down
25 changes: 13 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ use core::{
ops::Deref,
};

#[cfg(zerocopy_core_error)]
use core::error::Error;
#[cfg(all(not(zerocopy_core_error), any(feature = "std", test)))]
use std::error::Error;

use crate::{util::SendSyncPhantomData, KnownLayout, TryFromBytes};
#[cfg(doc)]
use crate::{FromBytes, Ref};
Expand Down Expand Up @@ -126,9 +131,8 @@ impl<A: fmt::Display, S: fmt::Display, V: fmt::Display> fmt::Display for Convert
}
}

#[cfg(any(feature = "std", test))]
#[allow(clippy::std_instead_of_core)]
impl<A, S, V> std::error::Error for ConvertError<A, S, V>
#[cfg(any(zerocopy_core_error, feature = "std", test))]
impl<A, S, V> Error for ConvertError<A, S, V>
where
A: fmt::Display + fmt::Debug,
S: fmt::Display + fmt::Debug,
Expand Down Expand Up @@ -228,9 +232,8 @@ where
}
}

#[cfg(any(feature = "std", test))]
#[allow(clippy::std_instead_of_core)]
impl<Src, Dst: ?Sized> std::error::Error for AlignmentError<Src, Dst>
#[cfg(any(zerocopy_core_error, feature = "std", test))]
impl<Src, Dst: ?Sized> Error for AlignmentError<Src, Dst>
where
Src: Deref,
Dst: KnownLayout,
Expand Down Expand Up @@ -353,9 +356,8 @@ where
}
}

#[cfg(any(feature = "std", test))]
#[allow(clippy::std_instead_of_core)]
impl<Src, Dst: ?Sized> std::error::Error for SizeError<Src, Dst>
#[cfg(any(zerocopy_core_error, feature = "std", test))]
impl<Src, Dst: ?Sized> Error for SizeError<Src, Dst>
where
Src: Deref,
Dst: KnownLayout,
Expand Down Expand Up @@ -441,9 +443,8 @@ where
}
}

#[cfg(any(feature = "std", test))]
#[allow(clippy::std_instead_of_core)]
impl<Src, Dst: ?Sized> std::error::Error for ValidityError<Src, Dst>
#[cfg(any(zerocopy_core_error, feature = "std", test))]
impl<Src, Dst: ?Sized> Error for ValidityError<Src, Dst>
where
Src: Deref,
Dst: KnownLayout + TryFromBytes,
Expand Down

0 comments on commit a494218

Please sign in to comment.