Skip to content

Add SavedAsPointer and it's mutable part as trait #456

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
49 changes: 49 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,52 @@ impl<T: FnOnce()> Drop for ScopeGuard<T> {
}
}
}

/// Trait for wrappers which are holding a direct pointer that they are getting from a C side function.
pub trait SavedAsPointer {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you can't use PointerWrapper? We should unify all these pointer wrappers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My Understanding of PointerWrapper is that it is designed for rust types which can be given to a C interface. The trait I am implementing is designed to hold a pointer which got allocated via a C api.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wedsonaf is my understanding of PointerWrapper correct? if not, should I withdraw this pr?

/// The underlying C type from [`crate::bindings`] which this Wrapper is containing.
type InternalType;

/// Regturns the raw pointer stored in the wrapper.
fn get_pointer(&self) -> *const Self::InternalType;

/// Returns the instance back which contains the raw pointer `ptr`.
///
/// # Safety
///
/// The passed pointer must be valid and of type `*const [`Self::InternalType`]`.
unsafe fn from_pointer(ptr: *const Self::InternalType) -> Self;

/// Returns a reference to to the internal struct
fn get_internal(&self) -> &Self::InternalType {
let ptr = self.get_pointer();

// SAFETY: self must be constructed via [`Self::from_pointer`].
unsafe { ptr.as_ref() }.unwrap()
}
}

/// Trait for wrappers which are holding a mutable direct pointer that they are getting from a C side function.
///
/// Extends [`SavedAsPointer`] with the addition of mutability.
pub trait SavedAsPointerMut: SavedAsPointer {
/// Return a mutable pointer to the underlying struct.
fn get_pointer_mut(&mut self) -> *mut Self::InternalType {
self.get_pointer() as *mut Self::InternalType
}

/// Returns the instance back which contains the raw pointer `ptr`.
///
/// # Safety
///
/// The passed pointer must be valid and of type `*mut [`Self::InternalType`]`.
unsafe fn from_pointer_mut(ptr: *mut Self::InternalType) -> Self;

// TODO: can we implement Self::from_pointer for the trait SavedAsPointer?
/// Returns a mutable reference to the internal struct.
fn get_internal_mut(&mut self) -> &mut Self::InternalType {
let ptr = self.get_pointer() as *mut Self::InternalType;

unsafe { ptr.as_mut() }.unwrap()
}
}