Skip to content

Commit

Permalink
Update to zeroize v1.5.0-pre and provide backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 9, 2022
1 parent 6fea767 commit f6366f0
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 106 deletions.
4 changes: 3 additions & 1 deletion .config/topic.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
11
13
Changelog
CHANGELOG
destructure/G
Expand All @@ -10,3 +10,5 @@ std's
struct/S
TODO
tuple
v1
zeroize
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Support `ZeroizeOnDrop`.
- Support [`ZeroizeOnDrop`](https://docs.rs/zeroize/1.5.0-pre/zeroize/trait.ZeroizeOnDrop.html).

### Removed
- **Breaking Change**: Remove support for `Zeroize(drop)`.
Expand Down
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ name = "derive-where"
readme = "README.md"
repository = "https://github.com/ModProg/derive-where"
rust-version = "1.34.2"
version = "1.0.0-rc.1"
version = "1.0.0-rc.2"

[lib]
proc-macro = true
Expand All @@ -24,6 +24,7 @@ proc-macro = true
nightly = []
safe = []
zeroize = []
zeroize-on-drop = ["zeroize"]

[dependencies]
proc-macro2 = { version = "1", default-features = false, features = ["proc-macro"] }
Expand All @@ -39,9 +40,6 @@ syn = { version = "1", default-features = false, features = [
[dev-dependencies]
syn = { version = "1", default-features = false, features = ["extra-traits"] }

[patch.crates-io]
zeroize = { git = "https://github.com/khonsulabs/utils", branch = "zeroize-on-drop-auto-deref" }

[[test]]
name = "util"
path = "tests/util.rs"
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ assert_eq!(test.0, 0);

### `ZeroizeOnDrop` options

If the `zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`]
and can be implemented without [`Zeroize`], otherwise it only implements
[`Drop`](https://doc.rust-lang.org/core/ops/trait.Drop.html) and requires [`Zeroize`] to be implemented.

[`ZeroizeOnDrop`] has one option:
- `crate`: an item-level option which specifies a path to the `zeroize`
crate in case of a re-export or rename.
Expand All @@ -206,7 +210,9 @@ The following traits can be derived with derive-where:
- [`PartialEq`](https://doc.rust-lang.org/core/cmp/trait.PartialEq.html)
- [`PartialOrd`](https://doc.rust-lang.org/core/cmp/trait.PartialOrd.html)
- [`Zeroize`]: Only available with the `zeroize` crate feature.
- [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature.
- [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature. If the
`zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`],
otherwise it only implements [`Drop`](https://doc.rust-lang.org/core/ops/trait.Drop.html).

### Supported items

Expand All @@ -233,7 +239,9 @@ Unions only support [`Clone`](https://doc.rust-lang.org/core/clone/trait.Clone.h
replaces all cases of [`core::hint::unreachable_unchecked`](https://doc.rust-lang.org/core/hint/fn.unreachable_unchecked.html) in [`Ord`](https://doc.rust-lang.org/core/hint/fn.unreachable_unchecked.html),
[`PartialEq`](https://doc.rust-lang.org/core/cmp/trait.PartialEq.html) and [`PartialOrd`](https://doc.rust-lang.org/core/cmp/trait.PartialOrd.html), which is what std uses, with
[`unreachable`](https://doc.rust-lang.org/core/macro.unreachable.html).
- `zeroize`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`].
- `zeroize`: Allows deriving [`Zeroize`] and [`method@zeroize`] on [`Drop`](https://doc.rust-lang.org/core/ops/trait.Drop.html).
- `zeroize-on-drop`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`] and
requires [zeroize] v1.5.0-pre.

## MSRV

Expand Down Expand Up @@ -271,10 +279,11 @@ conditions.
[CHANGELOG]: https://github.com/ModProg/derive-where/blob/main/CHANGELOG.md
[LICENSE-MIT]: https://github.com/ModProg/derive-where/blob/main/LICENSE-MIT
[LICENSE-APACHE]: https://github.com/ModProg/derive-where/blob/main/LICENSE-APACHE
[zeroize]: https://crates.io/crates/zeroize/1.5.0-pre
[`Debug`]: https://doc.rust-lang.org/core/fmt/trait.Debug.html
[`Default`]: https://doc.rust-lang.org/core/default/trait.Default.html
[`Hash`]: https://doc.rust-lang.org/core/hash/trait.Hash.html
[`Zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
[`ZeroizeOnDrop`]: https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html
[`ZeroizeOnDrop`]: https://docs.rs/zeroize/1.5.0-pre/zeroize/trait.ZeroizeOnDrop.html
[`method@zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html#tymethod.zeroize
[#27]: https://github.com/ModProg/derive-where/issues/27
3 changes: 2 additions & 1 deletion ensure-no-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ version = "0.0.0"
nightly = ["derive-where/nightly"]
safe = ["derive-where/safe"]
zeroize = ["derive-where/zeroize", "zeroize_"]
zeroize-on-drop = ["derive-where/zeroize-on-drop", "zeroize"]

[dependencies]
derive-where = { path = ".." }
zeroize_ = { version = "1", package = "zeroize", default-features = false, optional = true }
zeroize_ = { version = "1.5.0-pre", package = "zeroize", default-features = false, optional = true }

[lib]
doctest = false
Expand Down
3 changes: 2 additions & 1 deletion non-msrv-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ version = "0.0.0"
nightly = ["derive-where/nightly"]
safe = ["derive-where/safe"]
zeroize = ["derive-where/zeroize"]
zeroize-on-drop = ["derive-where/zeroize-on-drop", "zeroize"]

[dependencies]
derive-where = { path = ".." }

[dev-dependencies]
trybuild = { version = "1", default-features = false }
zeroize_ = { version = "1", package = "zeroize", default-features = false }
zeroize_ = { version = "1.5.0-pre", package = "zeroize", default-features = false }

[lib]
doctest = false
Expand Down
6 changes: 4 additions & 2 deletions non-msrv-tests/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::{
};

#[cfg(feature = "zeroize")]
use zeroize_::{Zeroize, ZeroizeOnDrop};
use zeroize_::Zeroize;
#[cfg(feature = "zeroize-on-drop")]
use zeroize_::ZeroizeOnDrop;

pub struct Wrapper<T = ()> {
data: i32,
Expand Down Expand Up @@ -44,7 +46,7 @@ impl<T> Zeroize for Wrapper<T> {
#[cfg(feature = "zeroize")]
pub struct AssertZeroize<'a, T: Zeroize>(pub &'a T);

#[cfg(feature = "zeroize")]
#[cfg(feature = "zeroize-on-drop")]
pub struct AssertZeroizeOnDrop<'a, T: ZeroizeOnDrop>(pub &'a T);

pub fn test_drop<T>(value: T, fun: impl FnOnce(&T)) {
Expand Down
11 changes: 10 additions & 1 deletion non-msrv-tests/tests/zeroize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ mod util;

use std::{
marker::PhantomData,
mem,
ops::{Deref, DerefMut},
};

use derive_where::DeriveWhere;
use zeroize::Zeroize;

use self::util::{AssertZeroize, AssertZeroizeOnDrop, Wrapper};
#[cfg(feature = "zeroize-on-drop")]
use self::util::AssertZeroizeOnDrop;
use self::util::{AssertZeroize, Wrapper};

#[test]
fn basic() {
Expand Down Expand Up @@ -57,7 +60,9 @@ fn drop() {
let mut test = Test(42.into());

let _ = AssertZeroize(&test);
#[cfg(feature = "zeroize-on-drop")]
let _ = AssertZeroizeOnDrop(&test);
assert!(mem::needs_drop::<Test<()>>());

test.zeroize();

Expand Down Expand Up @@ -90,7 +95,9 @@ fn fqs() {
let mut test = Test(Fqs(42.into()));

let _ = AssertZeroize(&test);
#[cfg(feature = "zeroize-on-drop")]
let _ = AssertZeroizeOnDrop(&test);
assert!(mem::needs_drop::<Test<()>>());

test.zeroize();

Expand Down Expand Up @@ -124,7 +131,9 @@ fn deref() {
let mut test = Test::<()>(ZeroizeDeref(42, PhantomData));

let _ = AssertZeroize(&test);
#[cfg(feature = "zeroize-on-drop")]
let _ = AssertZeroizeOnDrop(&test);
assert!(mem::needs_drop::<Test<()>>());

test.zeroize();

Expand Down
51 changes: 34 additions & 17 deletions src/attr/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,33 +408,50 @@ impl DeriveTrait {
use DeriveTrait::*;

match self {
Clone => util::path_from_strs(&["core", "clone", "Clone"]),
Copy => util::path_from_strs(&["core", "marker", "Copy"]),
Debug => util::path_from_strs(&["core", "fmt", "Debug"]),
Default => util::path_from_strs(&["core", "default", "Default"]),
Eq => util::path_from_strs(&["core", "cmp", "Eq"]),
Hash => util::path_from_strs(&["core", "hash", "Hash"]),
Ord => util::path_from_strs(&["core", "cmp", "Ord"]),
PartialEq => util::path_from_strs(&["core", "cmp", "PartialEq"]),
PartialOrd => util::path_from_strs(&["core", "cmp", "PartialOrd"]),
Clone => util::path_from_root_and_strs(self.crate_(), &["clone", "Clone"]),
Copy => util::path_from_root_and_strs(self.crate_(), &["marker", "Copy"]),
Debug => util::path_from_root_and_strs(self.crate_(), &["fmt", "Debug"]),
Default => util::path_from_root_and_strs(self.crate_(), &["default", "Default"]),
Eq => util::path_from_root_and_strs(self.crate_(), &["cmp", "Eq"]),
Hash => util::path_from_root_and_strs(self.crate_(), &["hash", "Hash"]),
Ord => util::path_from_root_and_strs(self.crate_(), &["cmp", "Ord"]),
PartialEq => util::path_from_root_and_strs(self.crate_(), &["cmp", "PartialEq"]),
PartialOrd => util::path_from_root_and_strs(self.crate_(), &["cmp", "PartialOrd"]),
#[cfg(feature = "zeroize")]
Zeroize { .. } => util::path_from_root_and_strs(self.crate_(), &["Zeroize"]),
#[cfg(feature = "zeroize")]
ZeroizeOnDrop { .. } => util::path_from_root_and_strs(self.crate_(), &["ZeroizeOnDrop"]),
}
}

/// Returns the path to the root crate for this trait.
pub fn crate_(&self) -> Path {
use DeriveTrait::*;

match self {
Clone => util::path_from_strs(&["core"]),
Copy => util::path_from_strs(&["core"]),
Debug => util::path_from_strs(&["core"]),
Default => util::path_from_strs(&["core"]),
Eq => util::path_from_strs(&["core"]),
Hash => util::path_from_strs(&["core"]),
Ord => util::path_from_strs(&["core"]),
PartialEq => util::path_from_strs(&["core"]),
PartialOrd => util::path_from_strs(&["core"]),
#[cfg(feature = "zeroize")]
Zeroize { crate_, .. } => {
if let Some(crate_) = crate_ {
let mut crate_ = crate_.clone();
crate_.segments.push(util::path_segment("Zeroize"));
crate_
crate_.clone()
} else {
util::path_from_strs(&["zeroize", "Zeroize"])
util::path_from_strs(&["zeroize"])
}
}
#[cfg(feature = "zeroize")]
ZeroizeOnDrop { crate_, .. } => {
if let Some(crate_) = crate_ {
let mut crate_ = crate_.clone();
crate_.segments.push(util::path_segment("ZeroizeOnDrop"));
crate_
crate_.clone()
} else {
util::path_from_strs(&["zeroize", "ZeroizeOnDrop"])
util::path_from_strs(&["zeroize"])
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,16 @@
//!
//! ## `ZeroizeOnDrop` options
//!
//! If the `zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`]
//! and can be implemented without [`Zeroize`], otherwise it only implements
//! [`Drop`] and requires [`Zeroize`] to be implemented.
//!
//! [`ZeroizeOnDrop`] has one option:
//! - `crate`: an item-level option which specifies a path to the `zeroize`
//! crate in case of a re-export or rename.
//!
//! ```
//! # #[cfg(feature = "zeroize")]
//! # #[cfg(feature = "zeroize-on-drop")]
//! # {
//! # use std::marker::PhantomData;
//! # use derive_where::DeriveWhere;
Expand Down Expand Up @@ -266,7 +270,9 @@
//! - [`PartialEq`]
//! - [`PartialOrd`]
//! - [`Zeroize`]: Only available with the `zeroize` crate feature.
//! - [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature.
//! - [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature. If the
//! `zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`],
//! otherwise it only implements [`Drop`].
//!
//! ## Supported items
//!
Expand All @@ -293,7 +299,9 @@
//! replaces all cases of [`core::hint::unreachable_unchecked`] in [`Ord`],
//! [`PartialEq`] and [`PartialOrd`], which is what std uses, with
//! [`unreachable`].
//! - `zeroize`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`].
//! - `zeroize`: Allows deriving [`Zeroize`] and [`method@zeroize`] on [`Drop`].
//! - `zeroize-on-drop`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`] and
//! requires [zeroize] v1.5.0-pre.
//!
//! # MSRV
//!
Expand Down Expand Up @@ -331,11 +339,12 @@
//! [CHANGELOG]: https://github.com/ModProg/derive-where/blob/main/CHANGELOG.md
//! [LICENSE-MIT]: https://github.com/ModProg/derive-where/blob/main/LICENSE-MIT
//! [LICENSE-APACHE]: https://github.com/ModProg/derive-where/blob/main/LICENSE-APACHE
//! [zeroize]: https://crates.io/crates/zeroize/1.5.0-pre
//! [`Debug`]: core::fmt::Debug
//! [`Default`]: core::default::Default
//! [`Hash`]: core::hash::Hash
//! [`Zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
//! [`ZeroizeOnDrop`]: https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html
//! [`ZeroizeOnDrop`]: https://docs.rs/zeroize/1.5.0-pre/zeroize/trait.ZeroizeOnDrop.html
//! [`method@zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html#tymethod.zeroize
//! [#27]: https://github.com/ModProg/derive-where/issues/27
Expand Down
Loading

0 comments on commit f6366f0

Please sign in to comment.