Skip to content

Commit ac3d7e1

Browse files
larseggertNot-Nik
authored andcommitted
chore: Use derive_more to reduce boilerplate (#3290)
* chore: Use `derive_more` to reduce boilerplate Already a dependency in Gecko. Let's us reduce boilerplate quite a bit. * Fix * Address comments * Fix --------- Signed-off-by: Lars Eggert <lars@eggert.org>
1 parent 201fed9 commit ac3d7e1

11 files changed

Lines changed: 126 additions & 177 deletions

File tree

Cargo.lock

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ description = "Gecko API for NSS"
1212
repository = "https://github.com/mozilla/nss-rs"
1313

1414
[dependencies]
15+
derive_more = { version = "2.0", default-features = false, features = ["display", "debug", "deref", "deref_mut", "from", "into", "add", "as_ref"] }
1516
enum-map = { version = "2.7", default-features = false }
1617
once_cell = "1"
1718
# Sync with https://searchfox.org/mozilla-central/source/Cargo.lock 2024-02-08

src/aead.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// except according to those terms.
66

77
use std::{
8-
fmt,
98
os::raw::{c_char, c_int, c_uint},
109
ptr::null_mut,
1110
};
@@ -115,6 +114,8 @@ experimental_api!(SSL_AeadDecrypt(
115114
experimental_api!(SSL_DestroyAead(ctx: *mut SSLAeadContext));
116115
scoped_ptr!(AeadContext, SSLAeadContext, SSL_DestroyAead);
117116

117+
#[derive(derive_more::Debug)]
118+
#[debug("[AEAD Context]")]
118119
pub struct RealAead {
119120
ctx: AeadContext,
120121
}
@@ -249,12 +250,6 @@ impl AeadTrait for RealAead {
249250
}
250251
}
251252

252-
impl fmt::Debug for RealAead {
253-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
254-
write!(f, "[AEAD Context]")
255-
}
256-
}
257-
258253
/// All the nonces are the same length. Exploit that.
259254
pub const NONCE_LEN: usize = 12;
260255

src/aead_null.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// option. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7-
use std::fmt;
8-
97
use crate::{
108
aead::AeadTrait,
119
constants::{Cipher, Version},
@@ -15,6 +13,8 @@ use crate::{
1513

1614
pub const AEAD_NULL_TAG: &[u8] = &[0x0a; 16];
1715

16+
#[derive(derive_more::Debug)]
17+
#[debug("[NULL AEAD]")]
1818
pub struct AeadNull {}
1919

2020
impl AeadNull {
@@ -86,9 +86,3 @@ impl AeadTrait for AeadNull {
8686
self.decrypt_check(count, aad, data)
8787
}
8888
}
89-
90-
impl fmt::Debug for AeadNull {
91-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92-
write!(f, "[NULL AEAD]")
93-
}
94-
}

src/agent.rs

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
cell::RefCell,
1414
convert::{TryFrom as _, TryInto as _},
1515
ffi::{CStr, CString},
16-
fmt::{self, Debug, Display, Formatter, Write as _},
16+
fmt::{self, Debug, Formatter, Write as _},
1717
mem::MaybeUninit,
1818
ops::{Deref, DerefMut},
1919
os::raw::{c_uint, c_void},
@@ -438,7 +438,8 @@ impl SecretAgentInfo {
438438
}
439439

440440
/// `SecretAgent` holds the common parts of client and server.
441-
#[derive(Debug)]
441+
#[derive(Debug, derive_more::Display)]
442+
#[display("Agent {fd:p}")]
442443
#[expect(clippy::module_name_repetitions, reason = "This is OK.")]
443444
pub struct SecretAgent {
444445
fd: *mut prio::PRFileDesc,
@@ -1005,14 +1006,9 @@ impl Drop for SecretAgent {
10051006
}
10061007
}
10071008

1008-
impl Display for SecretAgent {
1009-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
1010-
write!(f, "Agent {:p}", self.fd)
1011-
}
1012-
}
1013-
1014-
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone)]
1009+
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, derive_more::AsRef)]
10151010
pub struct ResumptionToken {
1011+
#[as_ref([u8])]
10161012
token: Vec<u8>,
10171013
expiration_time: Instant,
10181014
}
@@ -1026,12 +1022,6 @@ impl Debug for ResumptionToken {
10261022
}
10271023
}
10281024

1029-
impl AsRef<[u8]> for ResumptionToken {
1030-
fn as_ref(&self) -> &[u8] {
1031-
&self.token
1032-
}
1033-
}
1034-
10351025
impl ResumptionToken {
10361026
#[must_use]
10371027
pub const fn new(token: Vec<u8>, expiration_time: Instant) -> Self {
@@ -1048,8 +1038,11 @@ impl ResumptionToken {
10481038
}
10491039

10501040
/// A TLS Client.
1051-
#[derive(Debug)]
1041+
#[derive(Debug, derive_more::Display, derive_more::Deref, derive_more::DerefMut)]
1042+
#[display("Client {:p}", "agent.fd")]
10521043
pub struct Client {
1044+
#[deref]
1045+
#[deref_mut]
10531046
agent: SecretAgent,
10541047

10551048
/// The name of the server we're attempting a connection to.
@@ -1202,25 +1195,6 @@ impl Client {
12021195
}
12031196
}
12041197

1205-
impl Deref for Client {
1206-
type Target = SecretAgent;
1207-
fn deref(&self) -> &SecretAgent {
1208-
&self.agent
1209-
}
1210-
}
1211-
1212-
impl DerefMut for Client {
1213-
fn deref_mut(&mut self) -> &mut SecretAgent {
1214-
&mut self.agent
1215-
}
1216-
}
1217-
1218-
impl Display for Client {
1219-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
1220-
write!(f, "Client {:p}", self.agent.fd)
1221-
}
1222-
}
1223-
12241198
/// `ZeroRttCheckResult` encapsulates the options for handling a `ClientHello`.
12251199
#[derive(Clone, Debug, PartialEq, Eq)]
12261200
pub enum ZeroRttCheckResult {
@@ -1265,8 +1239,11 @@ impl ZeroRttCheckState {
12651239
}
12661240
}
12671241

1268-
#[derive(Debug)]
1242+
#[derive(Debug, derive_more::Display, derive_more::Deref, derive_more::DerefMut)]
1243+
#[display("Server {:p}", "agent.fd")]
12691244
pub struct Server {
1245+
#[deref]
1246+
#[deref_mut]
12701247
agent: SecretAgent,
12711248
/// This holds the HRR callback context.
12721249
zero_rtt_check: Option<Pin<Box<ZeroRttCheckState>>>,
@@ -1462,25 +1439,6 @@ impl Server {
14621439
}
14631440
}
14641441

1465-
impl Deref for Server {
1466-
type Target = SecretAgent;
1467-
fn deref(&self) -> &SecretAgent {
1468-
&self.agent
1469-
}
1470-
}
1471-
1472-
impl DerefMut for Server {
1473-
fn deref_mut(&mut self) -> &mut SecretAgent {
1474-
&mut self.agent
1475-
}
1476-
}
1477-
1478-
impl Display for Server {
1479-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
1480-
write!(f, "Server {:p}", self.agent.fd)
1481-
}
1482-
}
1483-
14841442
/// A generic container for Client or Server.
14851443
#[derive(Debug)]
14861444
pub enum Agent {

src/agentio.rs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
use std::{
1313
cmp::min,
1414
convert::{TryFrom as _, TryInto as _},
15-
fmt::{self, Display, Formatter},
1615
mem,
17-
ops::Deref,
1816
os::raw::{c_uint, c_void},
1917
pin::Pin,
2018
ptr::{null, null_mut},
@@ -45,7 +43,8 @@ pub fn as_c_void<T: Unpin>(pin: &mut Pin<Box<T>>) -> *mut c_void {
4543
}
4644

4745
/// A slice of the output.
48-
#[derive(Default)]
46+
#[derive(Default, derive_more::Debug)]
47+
#[debug("Record {:?}:{:?} {}", epoch, ct, hex_with_len(&data[..]))]
4948
pub struct Record {
5049
pub epoch: Epoch,
5150
pub ct: ContentType,
@@ -76,19 +75,7 @@ impl Record {
7675
}
7776
}
7877

79-
impl fmt::Debug for Record {
80-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
81-
write!(
82-
f,
83-
"Record {:?}:{:?} {}",
84-
self.epoch,
85-
self.ct,
86-
hex_with_len(&self.data[..])
87-
)
88-
}
89-
}
90-
91-
#[derive(Debug, Default)]
78+
#[derive(Debug, Default, derive_more::Deref)]
9279
pub struct RecordList {
9380
records: Vec<Record>,
9481
}
@@ -130,13 +117,6 @@ impl RecordList {
130117
}
131118
}
132119

133-
impl Deref for RecordList {
134-
type Target = Vec<Record>;
135-
fn deref(&self) -> &Vec<Record> {
136-
&self.records
137-
}
138-
}
139-
140120
pub struct RecordListIter(std::vec::IntoIter<Record>);
141121

142122
impl Iterator for RecordListIter {
@@ -165,7 +145,8 @@ impl Drop for AgentIoInputContext<'_> {
165145
}
166146

167147
// TODO: Derive Default when MSRV >= 1.88 (Default for raw pointers stabilized in 1.88).
168-
#[derive(Debug)]
148+
#[derive(Debug, derive_more::Display)]
149+
#[display("AgentIoInput {input:p}")]
169150
struct AgentIoInput {
170151
// input is data that is read by TLS.
171152
input: *const u8,
@@ -221,13 +202,8 @@ impl AgentIoInput {
221202
}
222203
}
223204

224-
impl Display for AgentIoInput {
225-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
226-
write!(f, "AgentIoInput {:p}", self.input)
227-
}
228-
}
229-
230-
#[derive(Debug, Default)]
205+
#[derive(Debug, Default, derive_more::Display)]
206+
#[display("AgentIo")]
231207
pub struct AgentIo {
232208
// input collects the input we might provide to TLS.
233209
input: AgentIoInput,
@@ -259,12 +235,6 @@ impl AgentIo {
259235
}
260236
}
261237

262-
impl Display for AgentIo {
263-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
264-
write!(f, "AgentIo")
265-
}
266-
}
267-
268238
unsafe extern "C" fn agent_close(fd: PrFd) -> PrStatus {
269239
(*fd).secret = null_mut();
270240
if let Some(dtor) = (*fd).dtor {

src/ext.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use std::{
1313
cell::RefCell,
1414
convert::TryFrom as _,
15-
fmt::{self, Debug, Formatter},
1615
os::raw::{c_uint, c_void},
1716
pin::Pin,
1817
rc::Rc,
@@ -90,7 +89,10 @@ pub trait ExtensionHandler {
9089

9190
type BoxedExtensionHandler = Box<Rc<RefCell<dyn ExtensionHandler>>>;
9291

92+
#[derive(derive_more::Debug)]
93+
#[debug("ExtensionTracker: {:?}", extension)]
9394
pub struct ExtensionTracker {
95+
#[expect(dead_code, reason = "Used by derived Debug implementation")]
9496
extension: Extension,
9597
handler: Pin<Box<BoxedExtensionHandler>>,
9698
}
@@ -203,9 +205,3 @@ impl ExtensionTracker {
203205
Ok(tracker)
204206
}
205207
}
206-
207-
impl Debug for ExtensionTracker {
208-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
209-
write!(f, "ExtensionTracker: {:?}", self.extension)
210-
}
211-
}

0 commit comments

Comments
 (0)