std/os/windows/io/
mod.rs

1//! Windows-specific extensions to general I/O primitives.
2//!
3//! Just like raw pointers, raw Windows handles and sockets point to resources
4//! with dynamic lifetimes, and they can dangle if they outlive their resources
5//! or be forged if they're created from invalid values.
6//!
7//! This module provides three types for representing raw handles and sockets
8//! with different ownership properties: raw, borrowed, and owned, which are
9//! analogous to types used for representing pointers. These types reflect concepts of [I/O
10//! safety][io-safety] on Windows.
11//!
12//! | Type                   | Analogous to |
13//! | ---------------------- | ------------ |
14//! | [`RawHandle`]          | `*const _`   |
15//! | [`RawSocket`]          | `*const _`   |
16//! |                        |              |
17//! | [`BorrowedHandle<'a>`] | `&'a _`      |
18//! | [`BorrowedSocket<'a>`] | `&'a _`      |
19//! |                        |              |
20//! | [`OwnedHandle`]        | `Box<_>`     |
21//! | [`OwnedSocket`]        | `Box<_>`     |
22//!
23//! Like raw pointers, `RawHandle` and `RawSocket` values are primitive values.
24//! And in new code, they should be considered unsafe to do I/O on (analogous
25//! to dereferencing them). Rust did not always provide this guidance, so
26//! existing code in the Rust ecosystem often doesn't mark `RawHandle` and
27//! `RawSocket` usage as unsafe.
28//! Libraries are encouraged to migrate, either by adding `unsafe` to APIs
29//! that dereference `RawHandle` and `RawSocket` values, or by using to
30//! `BorrowedHandle`, `BorrowedSocket`, `OwnedHandle`, or `OwnedSocket`.
31//!
32//! Like references, `BorrowedHandle` and `BorrowedSocket` values are tied to a
33//! lifetime, to ensure that they don't outlive the resource they point to.
34//! These are safe to use. `BorrowedHandle` and `BorrowedSocket` values may be
35//! used in APIs which provide safe access to any system call except for
36//! `CloseHandle`, `closesocket`, or any other call that would end the
37//! dynamic lifetime of the resource without ending the lifetime of the
38//! handle or socket.
39//!
40//! `BorrowedHandle` and `BorrowedSocket` values may be used in APIs which
41//! provide safe access to `DuplicateHandle` and `WSADuplicateSocketW` and
42//! related functions, so types implementing `AsHandle`, `AsSocket`,
43//! `From<OwnedHandle>`, or `From<OwnedSocket>` should not assume they always
44//! have exclusive access to the underlying object.
45//!
46//! Like boxes, `OwnedHandle` and `OwnedSocket` values conceptually own the
47//! resource they point to, and free (close) it when they are dropped.
48//!
49//! See the [`io` module docs][io-safety] for a general explanation of I/O safety.
50//!
51//! [`BorrowedHandle<'a>`]: crate::os::windows::io::BorrowedHandle
52//! [`BorrowedSocket<'a>`]: crate::os::windows::io::BorrowedSocket
53//! [io-safety]: crate::io#io-safety
54
55#![stable(feature = "rust1", since = "1.0.0")]
56
57mod handle;
58mod raw;
59mod socket;
60
61#[stable(feature = "io_safety", since = "1.63.0")]
62pub use handle::*;
63#[stable(feature = "rust1", since = "1.0.0")]
64pub use raw::*;
65#[stable(feature = "io_safety", since = "1.63.0")]
66pub use socket::*;
67
68#[cfg(test)]
69mod tests;