1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! An [`std::io::Write`] adapter that appends into locked, zeroizing memory.
use ;
use crateSecureBytes;
/// An [`io::Write`] that appends everything written to it into a [`SecureBytes`].
///
/// Handing a secret to `serde_json::to_string`/`to_vec` leaves the plaintext in an
/// ordinary `String`/`Vec` that nothing zeroizes, and `impl Serialize` cannot wipe
/// that buffer for you — a `Serialize` impl only ever sees a generic
/// `serde::Serializer`. Building the serializer around this writer instead keeps the
/// only heap copy of the plaintext in memory that is locked while unused and zeroized
/// on drop. Growth cannot leave a stale copy behind either: `SecureVec::reserve`
/// zeroizes the old allocation after moving the elements.
///
/// # Example
///
/// ```
/// use secure_types::{SecureBytes, SecureBytesWriter};
/// use std::io::Write;
///
/// let mut buffer = SecureBytes::new_with_capacity(32).unwrap();
/// write!(SecureBytesWriter::new(&mut buffer), "secret").unwrap();
///
/// buffer.unlock_slice(|bytes| assert_eq!(bytes, b"secret"));
/// ```
///
/// This writer targets [`SecureBytes`]. If you want the result as a
/// [`SecureString`](crate::SecureString), follow up with `SecureString::try_from`,
/// which validates the UTF-8 in a single pass.
///
/// The `codec` feature's `encode` writes its own format into locked memory; this writer is
/// for pointing some *other* serializer at locked memory.