Skip to content

Commit eeea5c2

Browse files
committed
Change API to unsafe and add doc comments
1 parent 686964f commit eeea5c2

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

library/std/src/sys/unix/ext/net/ancillary.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ struct AncillaryDataIter<'a, T> {
137137
}
138138

139139
impl<'a, T> AncillaryDataIter<'a, T> {
140-
pub fn new(data: &'a [u8]) -> AncillaryDataIter<'a, T> {
140+
/// Create `AncillaryDataIter` struct to iterate through the data unit in the control message.
141+
///
142+
/// # Safety
143+
///
144+
/// `data` must contain a valid control message.
145+
unsafe fn new(data: &'a [u8]) -> AncillaryDataIter<'a, T> {
141146
AncillaryDataIter { data, phantom: PhantomData }
142147
}
143148
}
@@ -325,12 +330,24 @@ pub enum AncillaryData<'a> {
325330
}
326331

327332
impl<'a> AncillaryData<'a> {
328-
fn as_rights(data: &'a [u8]) -> Self {
333+
/// Create a `AncillaryData::ScmRights` variant.
334+
///
335+
/// # Safety
336+
///
337+
/// `data` must contain a valid control message and the control message must be type of
338+
/// `SOL_SOCKET` and level of `SCM_RIGHTS`.
339+
unsafe fn as_rights(data: &'a [u8]) -> Self {
329340
let ancillary_data_iter = AncillaryDataIter::new(data);
330341
let scm_rights = ScmRights(ancillary_data_iter);
331342
AncillaryData::ScmRights(scm_rights)
332343
}
333344

345+
/// Create a `AncillaryData::ScmCredentials` variant.
346+
///
347+
/// # Safety
348+
///
349+
/// `data` must contain a valid control message and the control message must be type of
350+
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDENTIALS`.
334351
#[cfg(any(
335352
doc,
336353
target_os = "android",
@@ -345,7 +362,7 @@ impl<'a> AncillaryData<'a> {
345362
target_os = "openbsd",
346363
target_env = "uclibc",
347364
))]
348-
fn as_credentials(data: &'a [u8]) -> Self {
365+
unsafe fn as_credentials(data: &'a [u8]) -> Self {
349366
let ancillary_data_iter = AncillaryDataIter::new(data);
350367
let scm_credentials = ScmCredentials(ancillary_data_iter);
351368
AncillaryData::ScmCredentials(scm_credentials)

0 commit comments

Comments
 (0)