Skip to content

Commit

Permalink
Add more niche-optimization tests.
Browse files Browse the repository at this point in the history
This ports in the new tests added in rust-lang/rust#96947.
  • Loading branch information
sunfishcode committed May 11, 2022
1 parent 93735f8 commit 17c7a58
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions tests/niche-optimizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@ use io_lifetimes::{BorrowedFd, OwnedFd};
use io_lifetimes::{BorrowedSocket, OwnedSocket};

#[cfg(unix)]
use std::os::unix::io::RawFd;
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::RawFd;
use std::os::wasi::io::{FromRawSocket, IntoRawSocket, RawFd};
#[cfg(windows)]
use std::os::windows::io::RawSocket;
use std::os::windows::io::{FromRawSocket, IntoRawSocket, RawSocket};

#[cfg(all(rustc_attrs, any(unix, target_os = "wasi")))]
#[test]
fn test_niche_optimizations() {
assert_eq!(size_of::<Option<OwnedFd>>(), size_of::<RawFd>());
assert_eq!(size_of::<Option<BorrowedFd<'static>>>(), size_of::<RawFd>());
unsafe {
assert_eq!(OwnedFd::from_raw_fd(RawFd::MIN).into_raw_fd(), RawFd::MIN);
assert_eq!(OwnedFd::from_raw_fd(RawFd::MAX).into_raw_fd(), RawFd::MAX);
assert_eq!(
Some(OwnedFd::from_raw_fd(RawFd::MIN))
.unwrap()
.into_raw_fd(),
RawFd::MIN
);
assert_eq!(
Some(OwnedFd::from_raw_fd(RawFd::MAX))
.unwrap()
.into_raw_fd(),
RawFd::MAX
);
}
}

#[cfg(all(rustc_attrs, windows))]
Expand All @@ -31,4 +47,26 @@ fn test_niche_optimizations_socket() {
size_of::<Option<BorrowedSocket<'static>>>(),
size_of::<RawSocket>(),
);
unsafe {
assert_eq!(
OwnedSocket::from_raw_socket(RawSocket::MIN).into_raw_socket(),
RawSocket::MIN
);
assert_eq!(
OwnedSocket::from_raw_socket(RawSocket::MAX).into_raw_socket(),
RawSocket::MAX
);
assert_eq!(
Some(OwnedSocket::from_raw_socket(RawSocket::MIN))
.unwrap()
.into_raw_socket(),
RawSocket::MIN
);
assert_eq!(
Some(OwnedSocket::from_raw_socket(RawSocket::MAX))
.unwrap()
.into_raw_socket(),
RawSocket::MAX
);
}
}

0 comments on commit 17c7a58

Please sign in to comment.