win_numa_sys/io_buf.rs
1// Copyright (c) 2026 Mike Grier
2// Copied from windows-ioring-sys src/numa_buffer_io.rs at b6ba19e5.
3//! `io-buffers`' I/O buffer traits, implemented for [`NumaBuffer`].
4//!
5//! # Why the impls are here
6//!
7//! A trait may be implemented for a type only by the crate that owns the trait or the crate that
8//! owns the type. `io-buffers` owns the traits and this crate owns the type, and `io-buffers` is
9//! portable and cannot depend on a Windows allocator, so the impls are here. They are two lines each
10//! and delegate to [`NumaBuffer`]'s inherent accessors, which are public, so a crate that owns a
11//! buffer trait of its own can implement it the same way.
12
13use io_buffers::{IoBuf, IoBufMut};
14
15use crate::NumaBuffer;
16
17// SAFETY: the allocation's address is fixed once `VirtualAllocExNuma` returns
18// it and does not move for the value's life; the requested length is fixed
19// too. `NumaBuffer` is `Send`, which `IoBuf` requires.
20unsafe impl IoBuf for NumaBuffer {
21 fn stable_ptr(&self) -> *const u8 {
22 self.as_ptr()
23 }
24
25 fn bytes_len(&self) -> usize {
26 self.len()
27 }
28}
29
30// SAFETY: a `NumaBuffer` uniquely owns its allocation, so `&mut self` is
31// exclusive access to the bytes; the address is the same one `stable_ptr`
32// reports, because both return the allocation's base.
33unsafe impl IoBufMut for NumaBuffer {
34 fn stable_mut_ptr(&mut self) -> *mut u8 {
35 self.as_mut_ptr()
36 }
37}
38
39#[cfg(test)]
40mod tests;