Skip to content

Commit 547fbe7

Browse files
authored
Add a module-level example comment to the shm module. (#1137)
1 parent 8da70cb commit 547fbe7

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

src/shm.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
11
//! POSIX shared memory
2+
//!
3+
//! # Example
4+
//!
5+
//! ```
6+
//! use rustix::fs::{ftruncate, Mode};
7+
//! use rustix::mm::{mmap, MapFlags, ProtFlags};
8+
//! use rustix::{io, shm};
9+
//! use std::mem::size_of;
10+
//! use std::ptr::null_mut;
11+
//!
12+
//! # fn example() -> io::Result<()> {
13+
//! // A type describing the data to be shared.
14+
//! #[repr(C)]
15+
//! struct MyBufferType {
16+
//! // ...
17+
//! }
18+
//!
19+
//! // Create the shared memory object.
20+
//! let shm_path = "/rustix-shm-example";
21+
//! let fd = shm::open(
22+
//! shm_path,
23+
//! shm::OFlags::CREATE | shm::OFlags::EXCL | shm::OFlags::RDWR,
24+
//! Mode::RUSR | Mode::WUSR,
25+
//! )?;
26+
//!
27+
//! // Resize the shared memory object to the size of our data.
28+
//! ftruncate(&fd, size_of::<MyBufferType>() as u64)?;
29+
//!
30+
//! // Map the shared memory object into our address space.
31+
//! //
32+
//! // SAFETY: We're creating a new mapping that's independent of any existing
33+
//! // memory allocations. There are interesting things to say about *using*
34+
//! // `ptr`, but that's for another safety comment.
35+
//! let ptr = unsafe {
36+
//! mmap(
37+
//! null_mut(),
38+
//! size_of::<MyBufferType>(),
39+
//! ProtFlags::READ | ProtFlags::WRITE,
40+
//! MapFlags::SHARED,
41+
//! &fd,
42+
//! 0,
43+
//! )?
44+
//! };
45+
//!
46+
//! // Use `ptr`...
47+
//!
48+
//! // Remove the shared memory object name.
49+
//! shm::unlink(shm_path)?;
50+
//! # Ok(())
51+
//! # }
52+
//! ```
253
354
#![allow(unused_qualifications)]
455

@@ -8,13 +59,13 @@ use crate::{backend, io, path};
859
use super::shm;
960
pub use crate::backend::fs::types::Mode;
1061
pub use crate::backend::shm::types::ShmOFlags as OFlags;
11-
#[deprecated(note = "Use OFlags.")]
62+
#[deprecated(note = "Use `shm::OFlags`.")]
1263
#[doc(hidden)]
1364
pub use crate::backend::shm::types::ShmOFlags;
14-
#[deprecated(note = "Use open.")]
65+
#[deprecated(note = "Use `shm::open`.")]
1566
#[doc(hidden)]
1667
pub use open as shm_open;
17-
#[deprecated(note = "Use unlink.")]
68+
#[deprecated(note = "Use `shm::unlink`.")]
1869
#[doc(hidden)]
1970
pub use unlink as shm_unlink;
2071

0 commit comments

Comments
 (0)