Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokio: make all windows docs visible in unix builds #5530

Merged
merged 4 commits into from Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions tokio/src/doc/os.rs
Expand Up @@ -23,6 +23,27 @@ pub mod windows {
unsafe fn from_raw_handle(handle: RawHandle) -> Self;
}

/// See [std::os::windows::io::RawSocket](https://doc.rust-lang.org/std/os/windows/io/type.RawSocket.html)
pub type RawSocket = crate::doc::NotDefinedHere;

/// See [std::os::windows::io::AsRawSocket](https://doc.rust-lang.org/std/os/windows/io/trait.AsRawSocket.html)
pub trait AsRawSocket {
/// See [std::os::windows::io::AsRawSocket::as_raw_socket](https://doc.rust-lang.org/std/os/windows/io/trait.AsRawSocket.html#tymethod.as_raw_socket)
fn as_raw_socket(&self) -> RawSocket;
}

/// See [std::os::windows::io::FromRawSocket](https://doc.rust-lang.org/std/os/windows/io/trait.FromRawSocket.html)
pub trait FromRawSocket {
/// See [std::os::windows::io::FromRawSocket::from_raw_socket](https://doc.rust-lang.org/std/os/windows/io/trait.FromRawSocket.html#tymethod.from_raw_socket)
unsafe fn from_raw_socket(sock: RawSocket) -> Self;
}

/// See [std::os::windows::io::IntoRawSocket](https://doc.rust-lang.org/std/os/windows/io/trait.IntoRawSocket.html)
pub trait IntoRawSocket {
/// See [std::os::windows::io::IntoRawSocket::into_raw_socket](https://doc.rust-lang.org/std/os/windows/io/trait.IntoRawSocket.html#tymethod.into_raw_socket)
fn into_raw_socket(self) -> RawSocket;
}

/// See [std::os::windows::io::BorrowedHandle](https://doc.rust-lang.org/std/os/windows/io/struct.BorrowedHandle.html)
pub type BorrowedHandle<'handle> = crate::doc::NotDefinedHere;

Expand All @@ -31,5 +52,14 @@ pub mod windows {
/// See [std::os::windows::io::AsHandle::as_handle](https://doc.rust-lang.org/std/os/windows/io/trait.AsHandle.html#tymethod.as_handle)
fn as_handle(&self) -> BorrowedHandle<'_>;
}

/// See [std::os::windows::io::BorrowedSocket](https://doc.rust-lang.org/std/os/windows/io/struct.BorrowedSocket.html)
pub type BorrowedSocket<'socket> = crate::doc::NotDefinedHere;

/// See [std::os::windows::io::AsSocket](https://doc.rust-lang.org/std/os/windows/io/trait.AsSocket.html)
pub trait AsSocket {
/// See [std::os::windows::io::AsSocket::as_socket](https://doc.rust-lang.org/std/os/windows/io/trait.AsSocket.html#tymethod.as_socket)
fn as_socket(&self) -> BorrowedSocket<'_>;
}
}
}
38 changes: 21 additions & 17 deletions tokio/src/fs/file.rs
Expand Up @@ -741,28 +741,32 @@ impl std::os::unix::io::FromRawFd for File {
}
}

#[cfg(windows)]
impl std::os::windows::io::AsRawHandle for File {
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
self.std.as_raw_handle()
cfg_windows! {
use crate::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle};
#[cfg(not(tokio_no_as_fd))]
use crate::os::windows::io::{AsHandle, BorrowedHandle};

impl AsRawHandle for File {
fn as_raw_handle(&self) -> RawHandle {
self.std.as_raw_handle()
}
}
}

#[cfg(all(windows, not(tokio_no_as_fd)))]
impl std::os::windows::io::AsHandle for File {
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
unsafe {
std::os::windows::io::BorrowedHandle::borrow_raw(
std::os::windows::io::AsRawHandle::as_raw_handle(self),
)
#[cfg(not(tokio_no_as_fd))]
impl AsHandle for File {
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe {
BorrowedHandle::borrow_raw(
AsRawHandle::as_raw_handle(self),
)
}
}
}
}

#[cfg(windows)]
impl std::os::windows::io::FromRawHandle for File {
unsafe fn from_raw_handle(handle: std::os::windows::io::RawHandle) -> Self {
StdFile::from_raw_handle(handle).into()
impl FromRawHandle for File {
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
StdFile::from_raw_handle(handle).into()
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions tokio/src/fs/mod.rs
Expand Up @@ -115,9 +115,7 @@ feature! {
pub use self::symlink::symlink;
}

feature! {
#![windows]

cfg_windows! {
mod symlink_dir;
pub use self::symlink_dir::symlink_dir;

Expand Down
13 changes: 6 additions & 7 deletions tokio/src/fs/open_options.rs
Expand Up @@ -10,6 +10,11 @@ use mock_open_options::MockOpenOptions as StdOpenOptions;
#[cfg(not(test))]
use std::fs::OpenOptions as StdOpenOptions;

#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
#[cfg(windows)]
use std::os::windows::fs::OpenOptionsExt;

/// Options and flags which can be used to configure how a file is opened.
///
/// This builder exposes the ability to configure how a [`File`] is opened and
Expand Down Expand Up @@ -399,8 +404,6 @@ impl OpenOptions {
feature! {
#![unix]

use std::os::unix::fs::OpenOptionsExt;

impl OpenOptions {
/// Sets the mode bits that a new file will be created with.
///
Expand Down Expand Up @@ -464,11 +467,7 @@ feature! {
}
}

feature! {
#![windows]

use std::os::windows::fs::OpenOptionsExt;

cfg_windows! {
impl OpenOptions {
/// Overrides the `dwDesiredAccess` argument to the call to [`CreateFile`]
/// with the specified value.
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/fs/symlink_dir.rs
Expand Up @@ -10,7 +10,7 @@ use std::path::Path;
///
/// This is an async version of [`std::os::windows::fs::symlink_dir`][std]
///
/// [std]: std::os::windows::fs::symlink_dir
/// [std]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_dir.html
pub async fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/fs/symlink_file.rs
Expand Up @@ -10,7 +10,7 @@ use std::path::Path;
///
/// This is an async version of [`std::os::windows::fs::symlink_file`][std]
///
/// [std]: std::os::windows::fs::symlink_file
/// [std]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html
pub async fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
Expand Down
9 changes: 3 additions & 6 deletions tokio/src/io/stderr.rs
Expand Up @@ -95,13 +95,10 @@ mod sys {
}
}

#[cfg(windows)]
mod sys {
cfg_windows! {
#[cfg(not(tokio_no_as_fd))]
use std::os::windows::io::{AsHandle, BorrowedHandle};
use std::os::windows::io::{AsRawHandle, RawHandle};

use super::Stderr;
use crate::os::windows::io::{AsHandle, BorrowedHandle};
use crate::os::windows::io::{AsRawHandle, RawHandle};

impl AsRawHandle for Stderr {
fn as_raw_handle(&self) -> RawHandle {
Expand Down
9 changes: 3 additions & 6 deletions tokio/src/io/stdin.rs
Expand Up @@ -70,13 +70,10 @@ mod sys {
}
}

#[cfg(windows)]
mod sys {
cfg_windows! {
#[cfg(not(tokio_no_as_fd))]
use std::os::windows::io::{AsHandle, BorrowedHandle};
use std::os::windows::io::{AsRawHandle, RawHandle};

use super::Stdin;
use crate::os::windows::io::{AsHandle, BorrowedHandle};
use crate::os::windows::io::{AsRawHandle, RawHandle};

impl AsRawHandle for Stdin {
fn as_raw_handle(&self) -> RawHandle {
Expand Down
9 changes: 3 additions & 6 deletions tokio/src/io/stdout.rs
Expand Up @@ -94,13 +94,10 @@ mod sys {
}
}

#[cfg(windows)]
mod sys {
cfg_windows! {
#[cfg(not(tokio_no_as_fd))]
use std::os::windows::io::{AsHandle, BorrowedHandle};
use std::os::windows::io::{AsRawHandle, RawHandle};

use super::Stdout;
use crate::os::windows::io::{AsHandle, BorrowedHandle};
use crate::os::windows::io::{AsRawHandle, RawHandle};

impl AsRawHandle for Stdout {
fn as_raw_handle(&self) -> RawHandle {
Expand Down
12 changes: 12 additions & 0 deletions tokio/src/macros/cfg.rs
Expand Up @@ -13,6 +13,18 @@ macro_rules! feature {
}
}

/// Enables Windows-specific code.
/// Use this macro instead of `cfg(windows)` to generate docs properly.
macro_rules! cfg_windows {
($($item:item)*) => {
$(
#[cfg(any(all(doc, docsrs), windows))]
#[cfg_attr(docsrs, doc(cfg(windows)))]
$item
)*
}
}

/// Enables enter::block_on.
macro_rules! cfg_block_on {
($($item:item)*) => {
Expand Down
8 changes: 4 additions & 4 deletions tokio/src/net/tcp/listener.rs
Expand Up @@ -437,10 +437,10 @@ cfg_unstable! {
}
}

#[cfg(windows)]
mod sys {
use super::TcpListener;
use std::os::windows::prelude::*;
cfg_windows! {
use crate::os::windows::io::{AsRawSocket, RawSocket};
#[cfg(not(tokio_no_as_fd))]
use crate::os::windows::io::{AsSocket, BorrowedSocket};

impl AsRawSocket for TcpListener {
fn as_raw_socket(&self) -> RawSocket {
Expand Down
61 changes: 31 additions & 30 deletions tokio/src/net/tcp/socket.rs
Expand Up @@ -8,12 +8,14 @@ use std::net::SocketAddr;
use std::os::unix::io::{AsFd, BorrowedFd};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
#[cfg(all(windows, not(tokio_no_as_fd)))]
use std::os::windows::io::{AsSocket, BorrowedSocket};
use std::time::Duration;

cfg_windows! {
use crate::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
#[cfg(not(tokio_no_as_fd))]
use crate::os::windows::io::{AsSocket, BorrowedSocket};
}

cfg_net! {
/// A TCP socket that has not yet been converted to a `TcpStream` or
/// `TcpListener`.
Expand Down Expand Up @@ -769,37 +771,36 @@ impl IntoRawFd for TcpSocket {
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpSocket {
fn into_raw_socket(self) -> RawSocket {
self.inner.into_raw_socket()
cfg_windows! {
impl IntoRawSocket for TcpSocket {
fn into_raw_socket(self) -> RawSocket {
self.inner.into_raw_socket()
}
}
}

#[cfg(windows)]
impl AsRawSocket for TcpSocket {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
impl AsRawSocket for TcpSocket {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
}
}
}

#[cfg(all(windows, not(tokio_no_as_fd)))]
impl AsSocket for TcpSocket {
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
#[cfg(not(tokio_no_as_fd))]
impl AsSocket for TcpSocket {
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
}

#[cfg(windows)]
impl FromRawSocket for TcpSocket {
/// Converts a `RawSocket` to a `TcpStream`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
unsafe fn from_raw_socket(socket: RawSocket) -> TcpSocket {
let inner = socket2::Socket::from_raw_socket(socket);
TcpSocket { inner }
impl FromRawSocket for TcpSocket {
/// Converts a `RawSocket` to a `TcpStream`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
unsafe fn from_raw_socket(socket: RawSocket) -> TcpSocket {
let inner = socket2::Socket::from_raw_socket(socket);
TcpSocket { inner }
}
}
}
8 changes: 4 additions & 4 deletions tokio/src/net/tcp/stream.rs
Expand Up @@ -1387,10 +1387,10 @@ mod sys {
}
}

#[cfg(windows)]
mod sys {
use super::TcpStream;
use std::os::windows::prelude::*;
cfg_windows! {
use crate::os::windows::io::{AsRawSocket, RawSocket};
#[cfg(not(tokio_no_as_fd))]
use crate::os::windows::io::{AsSocket, BorrowedSocket};

impl AsRawSocket for TcpStream {
fn as_raw_socket(&self) -> RawSocket {
Expand Down
8 changes: 4 additions & 4 deletions tokio/src/net/udp.rs
Expand Up @@ -1746,10 +1746,10 @@ mod sys {
}
}

#[cfg(windows)]
mod sys {
use super::UdpSocket;
use std::os::windows::prelude::*;
cfg_windows! {
use crate::os::windows::io::{AsRawSocket, RawSocket};
#[cfg(not(tokio_no_as_fd))]
use crate::os::windows::io::{AsSocket, BorrowedSocket};

impl AsRawSocket for UdpSocket {
fn as_raw_socket(&self) -> RawSocket {
Expand Down