8000 New design based on `*mut T` by phil-opp · Pull Request #22 · rust-osdev/volatile · GitHub
[go: up one dir, main page]

Skip to content

New design based on *mut T #22

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 17 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Redesign access types and adjust (doc)tests
  • Loading branch information
phil-opp committed Jun 13, 2021
commit 2ece021ac1e3cbcf0ddcc73f45a9f5cd19ec5e99
84 changes: 38 additions & 46 deletions src/access.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
pub trait Access {}

/// Helper trait that is implemented by [`ReadWrite`] and [`ReadOnly`].
pub trait Readable: UnsafelyReadable {}

/// Helper trait that is implemented by [`ReadWrite`] and [`WriteOnly`].
pub trait Writable: UnsafelyWritable {}

pub trait UnsafelyReadable {}

pub trait UnsafelyWritable {}

/// Zero-sized marker type for allowing both read and write access.
#[derive(Debug, Copy, Clone)]
pub struct ReadWrite;
impl Access for ReadWrite {}
impl Readable for ReadWrite {}
impl UnsafelyReadable for ReadWrite {}
impl Writable for ReadWrite {}
impl UnsafelyWritable for ReadWrite {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct NoAccess;

/// Zero-sized marker type for allowing only read access.
#[derive(Debug, Copy, Clone)]
pub struct ReadOnly;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct UnsafeAccess;

impl Access for ReadOnly {}
impl Readable for ReadOnly {}
impl UnsafelyReadable for ReadOnly {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SafeAccess;

/// Zero-sized marker type for allowing only write access.
#[derive(Debug, Copy, Clone)]
pub struct WriteOnly;
pub trait Unsafe {}
pub trait Safe: Unsafe {}

impl Access for WriteOnly {}
impl Writable for WriteOnly {}
impl UnsafelyWritable for WriteOnly {}
impl Unsafe for UnsafeAccess {}
impl Unsafe for SafeAccess {}
impl Safe for SafeAccess {}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Custom<R, W> {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Access<R, W> {
pub read: R,
pub write: W,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct NoAccess;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct SafeAccess;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct UnsafeAccess;
impl Access<SafeAccess, NoAccess> {
pub const fn read_only() -> ReadOnly {
Access {
read: SafeAccess,
write: NoAccess,
}
}

pub fn write_only() -> WriteOnly {
Access {
read: NoAccess,
write: SafeAccess,
}
}

pub fn read_write() -> ReadWrite {
Access {
read: SafeAccess,
write: SafeAccess,
}
}
}

impl<W> Readable for Custom<SafeAccess, W> {}
impl<W> UnsafelyReadable for Custom<SafeAccess, W> {}
impl<W> UnsafelyReadable for Custom<UnsafeAccess, W> {}
impl<R> Writable for Custom<R, SafeAccess> {}
impl<R> UnsafelyWritable for Custom<R, SafeAccess> {}
impl<R> UnsafelyWritable for Custom<R, UnsafeAccess> {}
pub type ReadOnly = Access<SafeAccess, NoAccess>;
pub type WriteOnly = Access<NoAccess, SafeAccess>;
pub type ReadWrite = Access<SafeAccess, SafeAccess>;
Loading
0