Skip to content

Commit aeb7cb7

Browse files
Merge pull request #192 from iqlusioninc/zeroize/fix-alloc-support
zeroize: Support stablized 'alloc' crate
2 parents e350397 + 258f2ee commit aeb7cb7

File tree

2 files changed

+62
-53
lines changed

2 files changed

+62
-53
lines changed

zeroize/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@
301301
)]
302302
#![doc(html_root_url = "https://docs.rs/zeroize/0.7.0")]
303303

304+
#[cfg(all(feature = "alloc", not(feature = "std")))]
305+
#[allow(unused_imports)] // rustc bug?
306+
#[macro_use]
307+
extern crate alloc;
308+
304309
#[cfg(any(feature = "std", test))]
305310
#[cfg_attr(test, macro_use)]
306311
extern crate std;

zeroize/tests/zeroize_derive.rs

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
11
//! Integration tests for `zeroize_derive` proc macros
22
3-
use zeroize::Zeroize;
4-
5-
#[derive(Zeroize)]
6-
struct ZeroizableTupleStruct([u8; 3]);
7-
8-
#[test]
9-
fn derive_tuple_struct_test() {
10-
let mut value = ZeroizableTupleStruct([1, 2, 3]);
11-
value.zeroize();
12-
assert_eq!(&value.0, &[0, 0, 0])
13-
}
14-
15-
#[derive(Zeroize)]
16-
struct ZeroizableStruct {
17-
string: String,
18-
vec: Vec<u8>,
19-
bytearray: [u8; 3],
20-
number: usize,
21-
boolean: bool,
22-
}
23-
24-
#[test]
25-
fn derive_struct_test() {
26-
let mut value = ZeroizableStruct {
27-
string: String::from("Hello, world!"),
28-
vec: vec![1, 2, 3],
29-
bytearray: [4, 5, 6],
30-
number: 42,
31-
boolean: true,
32-
};
33-
34-
value.zeroize();
35-
36-
assert!(value.string.is_empty());
37-
assert!(value.vec.is_empty());
38-
assert_eq!(&value.bytearray, &[0, 0, 0]);
39-
assert_eq!(value.number, 0);
40-
assert!(!value.boolean);
41-
}
42-
43-
/// Test that the custom macro actually derived `Drop` for `ZeroizableStruct`
44-
trait Droppable: Drop {}
45-
impl Droppable for ZeroizableStruct {}
46-
47-
/// Test that this successfully disables deriving a drop handler by defining
48-
/// a custom one which should conflict if the custom derive did too
49-
#[allow(dead_code)]
50-
#[derive(Zeroize)]
51-
#[zeroize(no_drop)]
52-
struct ZeroizeNoDropStruct([u8; 3]);
53-
54-
impl Drop for ZeroizeNoDropStruct {
55-
fn drop(&mut self) {}
3+
#[cfg(feature = "zeroize_derive")]
4+
mod custom_derive_tests {
5+
use zeroize::Zeroize;
6+
7+
#[derive(Zeroize)]
8+
struct ZeroizableTupleStruct([u8; 3]);
9+
10+
#[test]
11+
fn derive_tuple_struct_test() {
12+
let mut value = ZeroizableTupleStruct([1, 2, 3]);
13+
value.zeroize();
14+
assert_eq!(&value.0, &[0, 0, 0])
15+
}
16+
17+
#[derive(Zeroize)]
18+
struct ZeroizableStruct {
19+
string: String,
20+
vec: Vec<u8>,
21+
bytearray: [u8; 3],
22+
number: usize,
23+
boolean: bool,
24+
}
25+
26+
#[test]
27+
fn derive_struct_test() {
28+
let mut value = ZeroizableStruct {
29+
string: String::from("Hello, world!"),
30+
vec: vec![1, 2, 3],
31+
bytearray: [4, 5, 6],
32+
number: 42,
33+
boolean: true,
34+
};
35+
36+
value.zeroize();
37+
38+
assert!(value.string.is_empty());
39+
assert!(value.vec.is_empty());
40+
assert_eq!(&value.bytearray, &[0, 0, 0]);
41+
assert_eq!(value.number, 0);
42+
assert!(!value.boolean);
43+
}
44+
45+
/// Test that the custom macro actually derived `Drop` for `ZeroizableStruct`
46+
trait Droppable: Drop {}
47+
48+
impl Droppable for ZeroizableStruct {}
49+
50+
/// Test that this successfully disables deriving a drop handler by defining
51+
/// a custom one which should conflict if the custom derive did too
52+
#[allow(dead_code)]
53+
#[derive(Zeroize)]
54+
#[zeroize(no_drop)]
55+
struct ZeroizeNoDropStruct([u8; 3]);
56+
57+
impl Drop for ZeroizeNoDropStruct {
58+
fn drop(&mut self) {}
59+
}
5660
}

0 commit comments

Comments
 (0)