From 997805ff5b8ce93cc4c87cc38b15e4883c4f07e1 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 23 Dec 2020 12:12:30 +0100 Subject: [PATCH 1/2] Add methods to restrict access --- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 54d103d..a359377 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -746,6 +746,55 @@ where } } +/// Methods for restricting access. +impl Volatile { + /// 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 { + 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 { + Volatile { + reference: self.reference, + access: PhantomData, + } + } +} + impl fmt::Debug for Volatile where R: Deref, From aca085fe2233b2da3cf85768478cd55c2b7e68b1 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 23 Dec 2020 12:39:05 +0100 Subject: [PATCH 2/2] Update changelog for #19 --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index 229bc1a..6f149f6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,7 @@ # Unreleased +- Add methods to restrict access ([#19](https://github.com/rust-osdev/volatile/pull/19)) + # 0.4.2 – 2020-10-31 - Change `slice::check_range` to `RangeBounds::assert_len` ([#16](https://github.com/rust-osdev/volatile/pull/16))