Skip to content

Commit 034429c

Browse files
committed
fix(io): redo new AncillaryBuilder API
1 parent 0f1c13b commit 034429c

4 files changed

Lines changed: 25 additions & 27 deletions

File tree

compio-io/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ required-features = ["compat"]
5656
[[test]]
5757
name = "framed"
5858
required-features = ["codec-serde-json"]
59+
60+
[[test]]
61+
name = "ancillary"
62+
required-features = ["ancillary"]

compio-io/src/ancillary/mod.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
//! const TYPE: i32 = 2;
2222
//!
2323
//! // Build a buffer containing two `u32` ancillary messages.
24-
//! let mut builder = AncillaryBuf::<{ ancillary_space::<u32>() * 2 }>::builder();
24+
//! let mut buf = AncillaryBuf::<{ ancillary_space::<u32>() * 2 }>::new();
25+
//! let mut builder = buf.builder();
2526
//! builder.try_push(LEVEL, TYPE, 42u32).unwrap();
2627
//! builder.try_push(LEVEL, TYPE, 43u32).unwrap();
2728
//! assert!(builder.try_push(LEVEL, TYPE, 44u32).is_none()); // buffer is full
28-
//! let buf = builder.finish();
2929
//!
3030
//! // Read it back.
3131
//! unsafe {
@@ -128,22 +128,18 @@ impl<'a> Iterator for AncillaryIter<'a> {
128128
}
129129

130130
/// Helper to construct ancillary (control) messages.
131-
pub struct AncillaryBuilder<const N: usize> {
132-
buffer: Box<AncillaryBuf<N>>,
131+
pub struct AncillaryBuilder<'a, const N: usize> {
133132
inner: sys::CMsgIter,
133+
buffer: &'a mut AncillaryBuf<N>,
134134
}
135135

136-
impl<const N: usize> AncillaryBuilder<N> {
137-
fn new() -> Self {
138-
let mut buffer = Box::new(AncillaryBuf::new());
139-
let inner = sys::CMsgIter::new(buffer.as_uninit().as_ptr().cast(), buffer.buf_capacity());
140-
Self { buffer, inner }
141-
}
142-
143-
/// Finishes building, returns the ancillary buffer containing the
144-
/// constructed control messages.
145-
pub fn finish(self) -> AncillaryBuf<N> {
146-
*self.buffer
136+
impl<'a, const N: usize> AncillaryBuilder<'a, N> {
137+
fn new(buffer: &'a mut AncillaryBuf<N>) -> Self {
138+
// TODO: optimize zeroing
139+
buffer.as_uninit().fill(MaybeUninit::new(0));
140+
buffer.len = 0;
141+
let inner = sys::CMsgIter::new(buffer.as_ptr(), buffer.buf_capacity());
142+
Self { inner, buffer }
147143
}
148144

149145
/// Try to append a control message entry into the buffer. If the buffer
@@ -192,15 +188,14 @@ impl<const N: usize> AncillaryBuf<N> {
192188
}
193189
}
194190

195-
/// Creates an [`AncillaryBuilder`] for constructing ancillary messages into
196-
/// this buffer.
191+
/// Create [`AncillaryBuilder`] with this buffer. The buffer will be zeroed
192+
/// on creation.
197193
///
198194
/// # Panics
199195
///
200-
/// This function will panic if the buffer size `N` is too small to hold at
201-
/// least one control message header.
202-
pub fn builder() -> AncillaryBuilder<N> {
203-
AncillaryBuilder::new()
196+
/// This function will panic if this buffer is too short.
197+
pub fn builder(&mut self) -> AncillaryBuilder<'_, N> {
198+
AncillaryBuilder::new(self)
204199
}
205200
}
206201

compio-io/tests/ancillary.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use compio_io::ancillary::{AncillaryBuf, AncillaryIter, CMsgBuilder};
55

66
#[test]
77
fn test_cmsg() {
8-
let mut builder = AncillaryBuf::<64>::builder();
8+
let mut buf = AncillaryBuf::<64>::new();
9+
let mut builder = buf.builder();
910

1011
builder.try_push(0, 0, ()).unwrap(); // 16 / 12
1112
builder.try_push(1, 1, u32::MAX).unwrap(); // 16 + 4 + 4 / 12 + 4
1213
builder.try_push(2, 2, i64::MIN).unwrap(); // 16 + 8 / 12 + 8
13-
let buf = builder.finish();
1414
assert!(buf.buf_len() == 64 || buf.buf_len() == 48);
1515

1616
unsafe {
@@ -35,7 +35,7 @@ fn test_cmsg() {
3535
#[test]
3636
#[should_panic]
3737
fn invalid_buffer_length() {
38-
AncillaryBuf::<1>::builder();
38+
AncillaryBuf::<1>::new().builder();
3939
}
4040

4141
#[test]

compio-quic/src/socket.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ impl Socket {
367367
let is_ipv4 = transmit.destination.ip().to_canonical().is_ipv4();
368368
let ecn = transmit.ecn.map_or(0, |x| x as u8);
369369

370-
let mut builder = AncillaryBuf::<CMSG_LEN>::builder();
370+
let mut control = AncillaryBuf::<CMSG_LEN>::new();
371+
let mut builder = control.builder();
371372

372373
// ECN
373374
if is_ipv4 {
@@ -461,8 +462,6 @@ impl Socket {
461462
let _ = segment_size;
462463
}
463464

464-
let mut control = builder.finish();
465-
466465
let mut buffer = buffer.slice(0..transmit.size);
467466

468467
loop {

0 commit comments

Comments
 (0)