mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-16 02:39:55 +00:00
876059cfe0
Previously all of the into_raw_fd implementations only returns a copy of the inner RawFd, while still holding the ownership of the file descriptor when returning for into_raw_fd. Since `self` is dropped at the end of into_raw_fd, the returned file descriptor will actually be closed, render the function unuseable. The patch makes sure that into_raw_fd actually takes the ownership of the file descriptor all the way from the inner IoHandle. To achieve this, I have to use an Option in IoHandle to store the I/O source. It's not pretty, but I cannot come up with a better way.
41 lines
1 KiB
Rust
41 lines
1 KiB
Rust
#![cfg(unix)]
|
|
|
|
use async_std::io;
|
|
use async_std::os::unix::net::UnixDatagram;
|
|
use async_std::task;
|
|
|
|
const JULIUS_CAESAR: &[u8] = b"
|
|
Friends, Romans, countrymen - lend me your ears!
|
|
I come not to praise Caesar, but to bury him.
|
|
";
|
|
|
|
#[test]
|
|
fn send_recv() -> io::Result<()> {
|
|
task::block_on(async {
|
|
let (socket1, socket2) = UnixDatagram::pair().unwrap();
|
|
socket1.send(JULIUS_CAESAR).await?;
|
|
|
|
let mut buf = vec![0; 1024];
|
|
let n = socket2.recv(&mut buf).await?;
|
|
assert_eq!(&buf[..n], JULIUS_CAESAR);
|
|
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn into_raw_fd() -> io::Result<()> {
|
|
use async_std::os::unix::io::{FromRawFd, IntoRawFd};
|
|
task::block_on(async {
|
|
let (socket1, socket2) = UnixDatagram::pair().unwrap();
|
|
socket1.send(JULIUS_CAESAR).await?;
|
|
|
|
let mut buf = vec![0; 1024];
|
|
|
|
let socket2 = unsafe { UnixDatagram::from_raw_fd(socket2.into_raw_fd()) };
|
|
let n = socket2.recv(&mut buf).await?;
|
|
assert_eq!(&buf[..n], JULIUS_CAESAR);
|
|
|
|
Ok(())
|
|
})
|
|
}
|