Skip to content

Commit 20db3f3

Browse files
authored
Correct a comment (#2282)
And while here, make cmsg_space a const fn, though I doubt it will make a difference since it's already inline. The original comment was _almost_ correct. Changing the cmsg_space macro to return an array works in most locations. However, it fails in the test_recverr_impl function in the tests due to a E0401 Error "Inner items do not inherit the generic parameters from the items they are embedded in." That error is very difficult to fix in our test, and it might occur in user code, too. The root of the problem is that each length of array is its own distinct type, and in a generic function cmsg_space returns an array type that may depend on the generic parameters. Alternatively we could provide a second macro that always heap allocates. But that may be too complicated just to save a single allocation.
1 parent aeea7cf commit 20db3f3

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

src/sys/socket/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,6 @@ feature! {
567567
/// let _ = cmsg_space!(RawFd, TimeVal);
568568
/// # }
569569
/// ```
570-
// Unfortunately, CMSG_SPACE isn't a const_fn, or else we could return a
571-
// stack-allocated array.
572570
#[macro_export]
573571
macro_rules! cmsg_space {
574572
( $( $x:ty ),* ) => {
@@ -581,7 +579,7 @@ macro_rules! cmsg_space {
581579

582580
#[inline]
583581
#[doc(hidden)]
584-
pub fn cmsg_space<T>() -> usize {
582+
pub const fn cmsg_space<T>() -> usize {
585583
// SAFETY: CMSG_SPACE is always safe
586584
unsafe { libc::CMSG_SPACE(mem::size_of::<T>() as libc::c_uint) as usize }
587585
}

0 commit comments

Comments
 (0)