Skip to content

Commit

Permalink
Correct a comment (#2282)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
asomers authored Jan 7, 2024
1 parent aeea7cf commit 20db3f3
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,6 @@ feature! {
/// let _ = cmsg_space!(RawFd, TimeVal);
/// # }
/// ```
// Unfortunately, CMSG_SPACE isn't a const_fn, or else we could return a
// stack-allocated array.
#[macro_export]
macro_rules! cmsg_space {
( $( $x:ty ),* ) => {
Expand All @@ -581,7 +579,7 @@ macro_rules! cmsg_space {

#[inline]
#[doc(hidden)]
pub fn cmsg_space<T>() -> usize {
pub const fn cmsg_space<T>() -> usize {
// SAFETY: CMSG_SPACE is always safe
unsafe { libc::CMSG_SPACE(mem::size_of::<T>() as libc::c_uint) as usize }
}
Expand Down

0 comments on commit 20db3f3

Please sign in to comment.