Repository: tokio-rs/tokio
File: tokio/src/signal/unix.rs (Rust)

Code before:
use mio::net::UnixStream;
use std::io::{self, Error, ErrorKind, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Once;
use std::task::{Context, Poll};

// There are reliable signals ranging from 1 to 33 available on every Unix platform.
#[cfg(not(any(target_os = "linux", target_os = "illumos")))]
pub(crate) type OsStorage = [SignalInfo; 34];

#[cfg(not(any(target_os = "linux", target_os = "illumos")))]
impl Init for OsStorage {
    fn init() -> Self {
        use std::array;

        array::from_fn(|_| SignalInfo::default())
    }
}

// On Linux and illumos, there are additional real-time signals available.
// (This is also likely true on Solaris, but this should be verified before
// being enabled.)
#[cfg(any(target_os = "linux", target_os = "illumos"))]
pub(crate) type OsStorage = Box<[SignalInfo]>;

#[cfg(any(target_os = "linux", target_os = "illumos"))]
impl Init for OsStorage {
    fn init() -> Self {
        use std::iter;

        iter::repeat_with(SignalInfo::default)
            .take(libc::SIGRTMAX() as usize + 1)
            .collect()
    }
}

impl Storage for OsStorage {
    fn event_info(&self, id: EventId) -> Option<&EventInfo> {
        self.get(id).map(|si| &si.event_info)
    }

    fn for_each<'a, F>(&'a self, f: F)
    where
        F: FnMut(&'a EventInfo),
    {
        self.iter().map(|si| &si.event_info).for_each(f);
    }
}

#[derive(Debug)]
pub(crate) struct OsExtraData {

Review comment (suggestion):
It may be clearer to move the `#[cfg()]` inside `fn init()` rather than having entirely separate blocks.

Code after:
use mio::net::UnixStream;
use std::io::{self, Error, ErrorKind, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Once;
use std::task::{Context, Poll};

#[cfg(not(any(target_os = "linux", target_os = "illumos")))]
pub(crate) type OsStorage = [SignalInfo; 34];

#[cfg(any(target_os = "linux", target_os = "illumos"))]
pub(crate) type OsStorage = Box<[SignalInfo]>;

impl Init for OsStorage {
    fn init() -> Self {
        // There are reliable signals ranging from 1 to 33 available on every Unix platform.
        #[cfg(not(any(target_os = "linux", target_os = "illumos")))]
        return std::array::from_fn(|_| SignalInfo::default());

        // On Linux and illumos, there are additional real-time signals
        // available. (This is also likely true on Solaris, but this should be
        // verified before being enabled.)
        #[cfg(any(target_os = "linux", target_os = "illumos"))]
        return std::iter::repeat_with(SignalInfo::default)
            .take(libc::SIGRTMAX() as usize + 1)
            .collect();
    }
}

impl Storage for OsStorage {
    fn event_info(&self, id: EventId) -> Option<&EventInfo> {
        self.get(id).map(|si| &si.event_info)
    }

    fn for_each<'a, F>(&'a self, f: F)
    where
        F: FnMut(&'a EventInfo),
    {
        self.iter().map(|si| &si.event_info).for_each(f);
    }
}

#[derive(Debug)]
pub(crate) struct OsExtraData {
    sender: UnixStream,
    pub(crate) receiver: UnixStream,
}

impl Init for OsExtraData {
    fn init() -> Self {
        let (receiver, sender) = UnixStream::pair().expect("failed to create UnixStream");