Skip to content

Commit 15b6441

Browse files
committed
Support using SocketAncillary with raw syscalls with unsafe functions.
1 parent ed33787 commit 15b6441

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

library/std/src/os/unix/net/ancillary.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,34 @@ impl<'a> SocketAncillary<'a> {
429429
self.buffer.len()
430430
}
431431

432+
/// Returns the raw ancillary data as byte slice.
433+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
434+
pub fn data(&self) -> &[u8] {
435+
&self.buffer[..self.length]
436+
}
437+
438+
/// Returns the entire buffer, including unused capacity.
439+
///
440+
/// Use [`data()`](Self::data) if you are only interested in the used portion of the buffer.
441+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
442+
pub fn buffer(&self) -> &[u8] {
443+
self.buffer
444+
}
445+
446+
/// Returns the entire buffer as mutable slice, including unused capacity.
447+
///
448+
/// You should normally call [`set_len()`](Self::set_len)
449+
/// after changing the contents of the buffer.
450+
///
451+
/// # Safety
452+
/// All data written to the buffer must be valid ancillary data for the target platform,
453+
/// and you must call [`set_len()`](Self::set_len) after changing
454+
/// the buffer contents to update the internal bookkeeping.
455+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
456+
pub unsafe fn buffer_mut(&mut self) -> &mut [u8] {
457+
self.buffer
458+
}
459+
432460
/// Returns `true` if the ancillary data is empty.
433461
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
434462
pub fn is_empty(&self) -> bool {
@@ -441,6 +469,20 @@ impl<'a> SocketAncillary<'a> {
441469
self.length
442470
}
443471

472+
/// Set the number of valid ancillary data bytes and the truncated flag.
473+
///
474+
/// This can be used with [`buffer_mut()`](Self::buffer_mut)
475+
/// to manually write ancillary data into the buffer.
476+
///
477+
/// # Safety
478+
/// - The length may not exceed [`capacity()`](Self::capacity).
479+
/// - The data in the buffer at `0..length` must be valid ancillary data.
480+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
481+
pub unsafe fn set_len(&mut self, length: usize, truncated: bool) {
482+
self.length = length;
483+
self.truncated = truncated;
484+
}
485+
444486
/// Returns the iterator of the control messages.
445487
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
446488
pub fn messages(&self) -> Messages<'_> {

0 commit comments

Comments
 (0)