forked from compio-rs/compio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
143 lines (125 loc) · 3.87 KB
/
mod.rs
File metadata and controls
143 lines (125 loc) · 3.87 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Ancillary data (control message) support for connected streams.
//!
//! Ancillary messages are used to pass out-of-band information such as file
//! descriptors (Unix domain sockets), credentials, or kTLS record types.
//!
//! # Types
//!
//! - [`AncillaryRef`]: A reference to a single ancillary data entry.
//! - [`AncillaryIter`]: An iterator over a buffer of ancillary messages.
//! - [`AncillaryBuilder`]: A builder for constructing ancillary messages into a
//! caller-supplied send buffer.
use std::{marker::PhantomData, mem::MaybeUninit};
cfg_if::cfg_if! {
if #[cfg(windows)] {
#[path = "windows.rs"]
mod sys;
} else if #[cfg(unix)] {
#[path = "unix.rs"]
mod sys;
}
}
/// Reference to an ancillary (control) message.
pub struct AncillaryRef<'a>(sys::CMsgRef<'a>);
impl AncillaryRef<'_> {
/// Returns the level of the control message.
pub fn level(&self) -> i32 {
self.0.level()
}
/// Returns the type of the control message.
pub fn ty(&self) -> i32 {
self.0.ty()
}
/// Returns the length of the control message.
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.0.len() as _
}
/// Returns a reference to the data of the control message.
///
/// # Safety
///
/// The data part must be properly aligned and contains an initialized
/// instance of `T`.
pub unsafe fn data<T>(&self) -> &T {
unsafe { self.0.data() }
}
}
/// An iterator for ancillary (control) messages.
pub struct AncillaryIter<'a> {
inner: sys::CMsgIter,
_p: PhantomData<&'a ()>,
}
impl<'a> AncillaryIter<'a> {
/// Create [`AncillaryIter`] with the given buffer.
///
/// # Panics
///
/// This function will panic if the buffer is too short or not properly
/// aligned.
///
/// # Safety
///
/// The buffer should contain valid control messages.
pub unsafe fn new(buffer: &'a [u8]) -> Self {
Self {
inner: sys::CMsgIter::new(buffer.as_ptr(), buffer.len()),
_p: PhantomData,
}
}
}
impl<'a> Iterator for AncillaryIter<'a> {
type Item = AncillaryRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
let cmsg = self.inner.current();
self.inner.next();
cmsg.map(AncillaryRef)
}
}
}
/// Helper to construct ancillary (control) messages.
pub struct AncillaryBuilder<'a> {
inner: sys::CMsgIter,
len: usize,
_p: PhantomData<&'a mut ()>,
}
impl<'a> AncillaryBuilder<'a> {
/// Create [`AncillaryBuilder`] with the given buffer. The buffer will be
/// zeroed on creation.
///
/// # Panics
///
/// This function will panic if the buffer is too short or not properly
/// aligned.
pub fn new(buffer: &'a mut [MaybeUninit<u8>]) -> Self {
// TODO: optimize zeroing
buffer.fill(MaybeUninit::new(0));
Self {
inner: sys::CMsgIter::new(buffer.as_ptr().cast(), buffer.len()),
len: 0,
_p: PhantomData,
}
}
/// Finishes building, returns length of the control message.
pub fn finish(self) -> usize {
self.len
}
/// Try to append a control message entry into the buffer. If the buffer
/// does not have enough space or is not properly aligned with the value
/// type, returns `None`.
pub fn try_push<T>(&mut self, level: i32, ty: i32, value: T) -> Option<()> {
if !self.inner.is_aligned::<T>() || !self.inner.is_space_enough::<T>() {
return None;
}
// SAFETY: the buffer is zeroed and the pointer is valid and aligned
unsafe {
let mut cmsg = self.inner.current_mut()?;
cmsg.set_level(level);
cmsg.set_ty(ty);
self.len += cmsg.set_data(value);
self.inner.next();
}
Some(())
}
}