Skip to content

Commit f6366f0

Browse files
committed
Update to zeroize v1.5.0-pre and provide backwards compatibility
1 parent 6fea767 commit f6366f0

File tree

13 files changed

+192
-106
lines changed

13 files changed

+192
-106
lines changed

.config/topic.dic

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
11
1+
13
22
Changelog
33
CHANGELOG
44
destructure/G
@@ -10,3 +10,5 @@ std's
1010
struct/S
1111
TODO
1212
tuple
13+
v1
14+
zeroize

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

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

1111
### Removed
1212
- **Breaking Change**: Remove support for `Zeroize(drop)`.

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ name = "derive-where"
1515
readme = "README.md"
1616
repository = "https://github.com/ModProg/derive-where"
1717
rust-version = "1.34.2"
18-
version = "1.0.0-rc.1"
18+
version = "1.0.0-rc.2"
1919

2020
[lib]
2121
proc-macro = true
@@ -24,6 +24,7 @@ proc-macro = true
2424
nightly = []
2525
safe = []
2626
zeroize = []
27+
zeroize-on-drop = ["zeroize"]
2728

2829
[dependencies]
2930
proc-macro2 = { version = "1", default-features = false, features = ["proc-macro"] }
@@ -39,9 +40,6 @@ syn = { version = "1", default-features = false, features = [
3940
[dev-dependencies]
4041
syn = { version = "1", default-features = false, features = ["extra-traits"] }
4142

42-
[patch.crates-io]
43-
zeroize = { git = "https://github.com/khonsulabs/utils", branch = "zeroize-on-drop-auto-deref" }
44-
4543
[[test]]
4644
name = "util"
4745
path = "tests/util.rs"

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ assert_eq!(test.0, 0);
181181

182182
### `ZeroizeOnDrop` options
183183

184+
If the `zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`]
185+
and can be implemented without [`Zeroize`], otherwise it only implements
186+
[`Drop`](https://doc.rust-lang.org/core/ops/trait.Drop.html) and requires [`Zeroize`] to be implemented.
187+
184188
[`ZeroizeOnDrop`] has one option:
185189
- `crate`: an item-level option which specifies a path to the `zeroize`
186190
crate in case of a re-export or rename.
@@ -206,7 +210,9 @@ The following traits can be derived with derive-where:
206210
- [`PartialEq`](https://doc.rust-lang.org/core/cmp/trait.PartialEq.html)
207211
- [`PartialOrd`](https://doc.rust-lang.org/core/cmp/trait.PartialOrd.html)
208212
- [`Zeroize`]: Only available with the `zeroize` crate feature.
209-
- [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature.
213+
- [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature. If the
214+
`zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`],
215+
otherwise it only implements [`Drop`](https://doc.rust-lang.org/core/ops/trait.Drop.html).
210216

211217
### Supported items
212218

@@ -233,7 +239,9 @@ Unions only support [`Clone`](https://doc.rust-lang.org/core/clone/trait.Clone.h
233239
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),
234240
[`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
235241
[`unreachable`](https://doc.rust-lang.org/core/macro.unreachable.html).
236-
- `zeroize`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`].
242+
- `zeroize`: Allows deriving [`Zeroize`] and [`method@zeroize`] on [`Drop`](https://doc.rust-lang.org/core/ops/trait.Drop.html).
243+
- `zeroize-on-drop`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`] and
244+
requires [zeroize] v1.5.0-pre.
237245

238246
## MSRV
239247

@@ -271,10 +279,11 @@ conditions.
271279
[CHANGELOG]: https://github.com/ModProg/derive-where/blob/main/CHANGELOG.md
272280
[LICENSE-MIT]: https://github.com/ModProg/derive-where/blob/main/LICENSE-MIT
273281
[LICENSE-APACHE]: https://github.com/ModProg/derive-where/blob/main/LICENSE-APACHE
282+
[zeroize]: https://crates.io/crates/zeroize/1.5.0-pre
274283
[`Debug`]: https://doc.rust-lang.org/core/fmt/trait.Debug.html
275284
[`Default`]: https://doc.rust-lang.org/core/default/trait.Default.html
276285
[`Hash`]: https://doc.rust-lang.org/core/hash/trait.Hash.html
277286
[`Zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
278-
[`ZeroizeOnDrop`]: https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html
287+
[`ZeroizeOnDrop`]: https://docs.rs/zeroize/1.5.0-pre/zeroize/trait.ZeroizeOnDrop.html
279288
[`method@zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html#tymethod.zeroize
280289
[#27]: https://github.com/ModProg/derive-where/issues/27

ensure-no-std/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ version = "0.0.0"
88
nightly = ["derive-where/nightly"]
99
safe = ["derive-where/safe"]
1010
zeroize = ["derive-where/zeroize", "zeroize_"]
11+
zeroize-on-drop = ["derive-where/zeroize-on-drop", "zeroize"]
1112

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

1617
[lib]
1718
doctest = false

non-msrv-tests/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ version = "0.0.0"
88
nightly = ["derive-where/nightly"]
99
safe = ["derive-where/safe"]
1010
zeroize = ["derive-where/zeroize"]
11+
zeroize-on-drop = ["derive-where/zeroize-on-drop", "zeroize"]
1112

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

1516
[dev-dependencies]
1617
trybuild = { version = "1", default-features = false }
17-
zeroize_ = { version = "1", package = "zeroize", default-features = false }
18+
zeroize_ = { version = "1.5.0-pre", package = "zeroize", default-features = false }
1819

1920
[lib]
2021
doctest = false

non-msrv-tests/tests/util.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::{
66
};
77

88
#[cfg(feature = "zeroize")]
9-
use zeroize_::{Zeroize, ZeroizeOnDrop};
9+
use zeroize_::Zeroize;
10+
#[cfg(feature = "zeroize-on-drop")]
11+
use zeroize_::ZeroizeOnDrop;
1012

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

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

5052
pub fn test_drop<T>(value: T, fun: impl FnOnce(&T)) {

non-msrv-tests/tests/zeroize.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ mod util;
66

77
use std::{
88
marker::PhantomData,
9+
mem,
910
ops::{Deref, DerefMut},
1011
};
1112

1213
use derive_where::DeriveWhere;
1314
use zeroize::Zeroize;
1415

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

1720
#[test]
1821
fn basic() {
@@ -57,7 +60,9 @@ fn drop() {
5760
let mut test = Test(42.into());
5861

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

6267
test.zeroize();
6368

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

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

95102
test.zeroize();
96103

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

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

129138
test.zeroize();
130139

src/attr/item.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -408,33 +408,50 @@ impl DeriveTrait {
408408
use DeriveTrait::*;
409409

410410
match self {
411-
Clone => util::path_from_strs(&["core", "clone", "Clone"]),
412-
Copy => util::path_from_strs(&["core", "marker", "Copy"]),
413-
Debug => util::path_from_strs(&["core", "fmt", "Debug"]),
414-
Default => util::path_from_strs(&["core", "default", "Default"]),
415-
Eq => util::path_from_strs(&["core", "cmp", "Eq"]),
416-
Hash => util::path_from_strs(&["core", "hash", "Hash"]),
417-
Ord => util::path_from_strs(&["core", "cmp", "Ord"]),
418-
PartialEq => util::path_from_strs(&["core", "cmp", "PartialEq"]),
419-
PartialOrd => util::path_from_strs(&["core", "cmp", "PartialOrd"]),
411+
Clone => util::path_from_root_and_strs(self.crate_(), &["clone", "Clone"]),
412+
Copy => util::path_from_root_and_strs(self.crate_(), &["marker", "Copy"]),
413+
Debug => util::path_from_root_and_strs(self.crate_(), &["fmt", "Debug"]),
414+
Default => util::path_from_root_and_strs(self.crate_(), &["default", "Default"]),
415+
Eq => util::path_from_root_and_strs(self.crate_(), &["cmp", "Eq"]),
416+
Hash => util::path_from_root_and_strs(self.crate_(), &["hash", "Hash"]),
417+
Ord => util::path_from_root_and_strs(self.crate_(), &["cmp", "Ord"]),
418+
PartialEq => util::path_from_root_and_strs(self.crate_(), &["cmp", "PartialEq"]),
419+
PartialOrd => util::path_from_root_and_strs(self.crate_(), &["cmp", "PartialOrd"]),
420+
#[cfg(feature = "zeroize")]
421+
Zeroize { .. } => util::path_from_root_and_strs(self.crate_(), &["Zeroize"]),
422+
#[cfg(feature = "zeroize")]
423+
ZeroizeOnDrop { .. } => util::path_from_root_and_strs(self.crate_(), &["ZeroizeOnDrop"]),
424+
}
425+
}
426+
427+
/// Returns the path to the root crate for this trait.
428+
pub fn crate_(&self) -> Path {
429+
use DeriveTrait::*;
430+
431+
match self {
432+
Clone => util::path_from_strs(&["core"]),
433+
Copy => util::path_from_strs(&["core"]),
434+
Debug => util::path_from_strs(&["core"]),
435+
Default => util::path_from_strs(&["core"]),
436+
Eq => util::path_from_strs(&["core"]),
437+
Hash => util::path_from_strs(&["core"]),
438+
Ord => util::path_from_strs(&["core"]),
439+
PartialEq => util::path_from_strs(&["core"]),
440+
PartialOrd => util::path_from_strs(&["core"]),
420441
#[cfg(feature = "zeroize")]
421442
Zeroize { crate_, .. } => {
422443
if let Some(crate_) = crate_ {
423-
let mut crate_ = crate_.clone();
424-
crate_.segments.push(util::path_segment("Zeroize"));
425-
crate_
444+
crate_.clone()
426445
} else {
427-
util::path_from_strs(&["zeroize", "Zeroize"])
446+
util::path_from_strs(&["zeroize"])
428447
}
429448
}
430449
#[cfg(feature = "zeroize")]
431450
ZeroizeOnDrop { crate_, .. } => {
432451
if let Some(crate_) = crate_ {
433-
let mut crate_ = crate_.clone();
434-
crate_.segments.push(util::path_segment("ZeroizeOnDrop"));
435-
crate_
452+
crate_.clone()
436453
} else {
437-
util::path_from_strs(&["zeroize", "ZeroizeOnDrop"])
454+
util::path_from_strs(&["zeroize"])
438455
}
439456
}
440457
}

src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,16 @@
217217
//!
218218
//! ## `ZeroizeOnDrop` options
219219
//!
220+
//! If the `zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`]
221+
//! and can be implemented without [`Zeroize`], otherwise it only implements
222+
//! [`Drop`] and requires [`Zeroize`] to be implemented.
223+
//!
220224
//! [`ZeroizeOnDrop`] has one option:
221225
//! - `crate`: an item-level option which specifies a path to the `zeroize`
222226
//! crate in case of a re-export or rename.
223227
//!
224228
//! ```
225-
//! # #[cfg(feature = "zeroize")]
229+
//! # #[cfg(feature = "zeroize-on-drop")]
226230
//! # {
227231
//! # use std::marker::PhantomData;
228232
//! # use derive_where::DeriveWhere;
@@ -266,7 +270,9 @@
266270
//! - [`PartialEq`]
267271
//! - [`PartialOrd`]
268272
//! - [`Zeroize`]: Only available with the `zeroize` crate feature.
269-
//! - [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature.
273+
//! - [`ZeroizeOnDrop`]: Only available with the `zeroize` crate feature. If the
274+
//! `zeroize-on-drop` feature is enabled, it implements [`ZeroizeOnDrop`],
275+
//! otherwise it only implements [`Drop`].
270276
//!
271277
//! ## Supported items
272278
//!
@@ -293,7 +299,9 @@
293299
//! replaces all cases of [`core::hint::unreachable_unchecked`] in [`Ord`],
294300
//! [`PartialEq`] and [`PartialOrd`], which is what std uses, with
295301
//! [`unreachable`].
296-
//! - `zeroize`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`].
302+
//! - `zeroize`: Allows deriving [`Zeroize`] and [`method@zeroize`] on [`Drop`].
303+
//! - `zeroize-on-drop`: Allows deriving [`Zeroize`] and [`ZeroizeOnDrop`] and
304+
//! requires [zeroize] v1.5.0-pre.
297305
//!
298306
//! # MSRV
299307
//!
@@ -331,11 +339,12 @@
331339
//! [CHANGELOG]: https://github.com/ModProg/derive-where/blob/main/CHANGELOG.md
332340
//! [LICENSE-MIT]: https://github.com/ModProg/derive-where/blob/main/LICENSE-MIT
333341
//! [LICENSE-APACHE]: https://github.com/ModProg/derive-where/blob/main/LICENSE-APACHE
342+
//! [zeroize]: https://crates.io/crates/zeroize/1.5.0-pre
334343
//! [`Debug`]: core::fmt::Debug
335344
//! [`Default`]: core::default::Default
336345
//! [`Hash`]: core::hash::Hash
337346
//! [`Zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
338-
//! [`ZeroizeOnDrop`]: https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html
347+
//! [`ZeroizeOnDrop`]: https://docs.rs/zeroize/1.5.0-pre/zeroize/trait.ZeroizeOnDrop.html
339348
//! [`method@zeroize`]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html#tymethod.zeroize
340349
//! [#27]: https://github.com/ModProg/derive-where/issues/27
341350

0 commit comments

Comments
 (0)