Add more niche-optimization tests.

This ports in the new tests added in rust-lang/rust#96947.
This commit is contained in:
Dan Gohman
2022-05-11 10:07:07 -07:00
committed by GitHub
parent 5244a3880f
commit e3e2a0138d
2 changed files with 41 additions and 4 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ use std::{
use io_lifetimes::{AsFd, FromFd, OwnedFd};
#[cfg(windows)]
use io_lifetimes::{AsHandle, FromHandle, InvalidHandleError, OwnedHandle};
use io_lifetimes::{AsHandle, FromHandle, OwnedHandle};
#[cfg(windows)]
use std::{convert::TryInto, os::windows::io::RawHandle, ptr::null_mut};
+40 -3
View File
@@ -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))]
@@ -31,4 +47,25 @@ fn test_niche_optimizations_socket() {
size_of::<Option<BorrowedSocket<'static>>>(),
size_of::<RawSocket>(),
);
unsafe {
#[cfg(target_pointer_width = "32")]
let (min, max) = (i32::MIN as u32, i32::MAX as u32);
#[cfg(target_pointer_width = "64")]
let (min, max) = (i64::MIN as u64, i64::MAX as u64);
assert_eq!(OwnedSocket::from_raw_socket(min).into_raw_socket(), min);
assert_eq!(OwnedSocket::from_raw_socket(max).into_raw_socket(), max);
assert_eq!(
Some(OwnedSocket::from_raw_socket(min))
.unwrap()
.into_raw_socket(),
min
);
assert_eq!(
Some(OwnedSocket::from_raw_socket(max))
.unwrap()
.into_raw_socket(),
max
);
}
}