Skip to content

Commit 36fd849

Browse files
authored
Add more niche-optimization tests.
This ports in the new tests added in rust-lang/rust#96947.
1 parent 93735f8 commit 36fd849

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

examples/hello.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
use io_lifetimes::{AsFd, FromFd, OwnedFd};
1717

1818
#[cfg(windows)]
19-
use io_lifetimes::{AsHandle, FromHandle, InvalidHandleError, OwnedHandle};
19+
use io_lifetimes::{AsHandle, FromHandle, OwnedHandle};
2020
#[cfg(windows)]
2121
use std::{convert::TryInto, os::windows::io::RawHandle, ptr::null_mut};
2222

tests/niche-optimizations.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,33 @@ use io_lifetimes::{BorrowedFd, OwnedFd};
1010
use io_lifetimes::{BorrowedSocket, OwnedSocket};
1111

1212
#[cfg(unix)]
13-
use std::os::unix::io::RawFd;
13+
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
1414
#[cfg(target_os = "wasi")]
15-
use std::os::wasi::io::RawFd;
15+
use std::os::wasi::io::{FromRawSocket, IntoRawSocket, RawFd};
1616
#[cfg(windows)]
17-
use std::os::windows::io::RawSocket;
17+
use std::os::windows::io::{FromRawSocket, IntoRawSocket, RawSocket};
1818

1919
#[cfg(all(rustc_attrs, any(unix, target_os = "wasi")))]
2020
#[test]
2121
fn test_niche_optimizations() {
2222
assert_eq!(size_of::<Option<OwnedFd>>(), size_of::<RawFd>());
2323
assert_eq!(size_of::<Option<BorrowedFd<'static>>>(), size_of::<RawFd>());
24+
unsafe {
25+
assert_eq!(OwnedFd::from_raw_fd(RawFd::MIN).into_raw_fd(), RawFd::MIN);
26+
assert_eq!(OwnedFd::from_raw_fd(RawFd::MAX).into_raw_fd(), RawFd::MAX);
27+
assert_eq!(
28+
Some(OwnedFd::from_raw_fd(RawFd::MIN))
29+
.unwrap()
30+
.into_raw_fd(),
31+
RawFd::MIN
32+
);
33+
assert_eq!(
34+
Some(OwnedFd::from_raw_fd(RawFd::MAX))
35+
.unwrap()
36+
.into_raw_fd(),
37+
RawFd::MAX
38+
);
39+
}
2440
}
2541

2642
#[cfg(all(rustc_attrs, windows))]
@@ -31,4 +47,25 @@ fn test_niche_optimizations_socket() {
3147
size_of::<Option<BorrowedSocket<'static>>>(),
3248
size_of::<RawSocket>(),
3349
);
50+
unsafe {
51+
#[cfg(target_pointer_width = "32")]
52+
let (min, max) = (i32::MIN as u32, i32::MAX as u32);
53+
#[cfg(target_pointer_width = "64")]
54+
let (min, max) = (i64::MIN as u64, i64::MAX as u64);
55+
56+
assert_eq!(OwnedSocket::from_raw_socket(min).into_raw_socket(), min);
57+
assert_eq!(OwnedSocket::from_raw_socket(max).into_raw_socket(), max);
58+
assert_eq!(
59+
Some(OwnedSocket::from_raw_socket(min))
60+
.unwrap()
61+
.into_raw_socket(),
62+
min
63+
);
64+
assert_eq!(
65+
Some(OwnedSocket::from_raw_socket(max))
66+
.unwrap()
67+
.into_raw_socket(),
68+
max
69+
);
70+
}
3471
}

0 commit comments

Comments
 (0)