1
1
mirror of https://github.com/wez/wezterm.git synced 2024-08-17 02:00:25 +03:00

expand socket descriptor concept

I didn't notice that windows has separate traits for raw sockets,
so blanket impls for things that returned RawHandle's were not
applying on windows in the same way that they were on unix systems.
This commit is contained in:
Wez Furlong 2019-06-19 11:11:06 -07:00
parent 240bc8033c
commit 5ac61ec8d3
4 changed files with 53 additions and 10 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "filedescriptor"
version = "0.3.0"
version = "0.4.0"
authors = ["Wez Furlong"]
edition = "2018"
repository = "https://github.com/wez/wzsh"

View File

@ -112,9 +112,6 @@ pub use crate::windows::*;
/// type.
pub trait AsRawFileDescriptor {
fn as_raw_file_descriptor(&self) -> RawFileDescriptor;
fn as_socket_descriptor(&self) -> SocketDescriptor {
self.as_raw_file_descriptor() as SocketDescriptor
}
}
/// `IntoRawFileDescriptor` is a platform independent trait for converting
@ -133,6 +130,16 @@ pub trait FromRawFileDescriptor {
unsafe fn from_raw_file_descriptor(fd: RawFileDescriptor) -> Self;
}
pub trait AsRawSocketDescriptor {
fn as_raw_socket_descriptor(&self) -> SocketDescriptor;
}
pub trait IntoRawSocketDescriptor {
fn into_raw_socket_descriptor(self) -> SocketDescriptor;
}
pub trait FromRawSocketDescriptor {
unsafe fn from_raw_socket_descriptor(fd: SocketDescriptor) -> Self;
}
/// `OwnedHandle` allows managing the lifetime of the platform `RawFileDescriptor`
/// type. It is exposed in the interface of this crate primarily for convenience
/// on Windows where the system handle type is used for a variety of objects

View File

@ -1,6 +1,6 @@
use crate::{
AsRawFileDescriptor, FileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, OwnedHandle,
Pipe,
AsRawFileDescriptor, AsRawSocketDescriptor, FileDescriptor, FromRawFileDescriptor,
FromRawSocketDescriptor, IntoRawFileDescriptor, IntoRawSocketDescriptor, OwnedHandle, Pipe,
};
use failure::{bail, Fallible};
use std::os::unix::prelude::*;
@ -33,6 +33,24 @@ impl<T: FromRawFd> FromRawFileDescriptor for T {
}
}
impl<T: AsRawFd> AsRawSocketDescriptor for T {
fn as_raw_socket_descriptor(&self) -> SocketDescriptor {
self.as_raw_fd()
}
}
impl<T: IntoRawFd> IntoRawSocketDescriptor for T {
fn into_raw_socket_descriptor(self) -> SocketDescriptor {
self.into_raw_fd()
}
}
impl<T: FromRawFd> FromRawSocketDescriptor for T {
unsafe fn from_raw_socket_descriptor(fd: SocketDescriptor) -> Self {
Self::from_raw_fd(fd)
}
}
impl Drop for OwnedHandle {
fn drop(&mut self) {
unsafe {
@ -232,10 +250,10 @@ pub fn socketpair_impl() -> Fallible<(FileDescriptor, FileDescriptor)> {
std::io::Error::last_os_error()
)
} else {
let mut read = FileDescriptor {
let read = FileDescriptor {
handle: OwnedHandle { handle: fds[0] },
};
let mut write = FileDescriptor {
let write = FileDescriptor {
handle: OwnedHandle { handle: fds[1] },
};
Ok((read, write))

View File

@ -1,6 +1,6 @@
use crate::{
AsRawFileDescriptor, FileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, OwnedHandle,
Pipe,
AsRawFileDescriptor, AsRawSocketDescriptor, FileDescriptor, FromRawFileDescriptor,
FromRawSocketDescriptor, IntoRawFileDescriptor, IntoRawSocketDescriptor, OwnedHandle, Pipe,
};
use failure::{bail, Fallible};
use std::io::{self, Error as IoError};
@ -52,6 +52,24 @@ impl<T: FromRawHandle> FromRawFileDescriptor for T {
}
}
impl<T: AsRawSocket> AsRawSocketDescriptor for T {
fn as_raw_socket_descriptor(&self) -> SocketDescriptor {
self.as_raw_socket() as SocketDescriptor
}
}
impl<T: IntoRawSocket> IntoRawSocketDescriptor for T {
fn into_raw_socket_descriptor(self) -> SocketDescriptor {
self.into_raw_socket() as SocketDescriptor
}
}
impl<T: FromRawSocket> FromRawSocketDescriptor for T {
unsafe fn from_raw_socket_descriptor(handle: SocketDescriptor) -> Self {
Self::from_raw_socket(handle as _)
}
}
unsafe impl Send for OwnedHandle {}
#[derive(Clone, Copy, PartialEq, Eq)]