10000 Add methods to restrict access by phil-opp · Pull Request #19 · rust-osdev/volatile · GitHub
[go: up one dir, main page]

Skip to content

Add methods to restrict access #19

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

Merged
merged 2 commits into from
Dec 23, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add methods to restrict access
  • Loading branch information
phil-opp committed Dec 23, 2020
commit 997805ff5b8ce93cc4c87cc38b15e4883c4f07e1
49 changes: 49 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,55 @@ where
}
}

/// Methods for restricting access.
impl<R> Volatile<R> {
/// Restricts access permissions to read-only.
///
/// ## Example
///
/// ```
/// use volatile::Volatile;
///
/// let mut value: i16 = -4;
/// let mut volatile = Volatile::new(&mut value);
///
/// let read_only = volatile.read_only();
/// assert_eq!(read_only.read(), -4);
/// // read_only.write(10); // compile-time error
/// ```
pub fn read_only(self) -> Volatile<R, ReadOnly> {
Volatile {
reference: self.reference,
access: PhantomData,
}
}

/// Restricts access permissions to write-only.
///
/// ## Example
///
/// Creating a write-only reference to a struct field:
///
/// ```
/// use volatile::Volatile;
///
/// struct Example { field_1: u32, field_2: u8, }
/// let mut value = Example { field_1: 15, field_2: 255 };
/// let mut volatile = Volatile::new(&mut value);
///
/// // construct a volatile write-only reference to `field_2`
/// let mut field_2 = volatile.map_mut(|example| &mut example.field_2).write_only();
/// field_2.write(14);
/// // field_2.read(); // compile-time error
/// ```
pub fn write_only(self) -> Volatile<R, WriteOnly> {
Volatile {
reference: self.reference,
access: PhantomData,
}
}
}

impl<R, T, A> fmt::Debug for Volatile<R, A>
where
R: Deref<Target = T>,
Expand Down
0