rust: types: Add `try_from_foreign()` method
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 rust: types: Add `try_from_foreign()` method
Currently ForeignOwnable::from_foreign() only works for non-null pointers for the existing impls (e.g. Box, Arc). It may create a few duplicate code like:
```rust
// p is a maybe null pointer
if p.is_null() {
None
} else {
Some(unsafe { T::from_foreign(p) })
}
``
--- rust/kernel/types.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
rust/kernel/types.rs +19 -0
@@ -46,6 +46,25 @@ pub trait ForeignOwnable: Sized {/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for /// this object must have been dropped. unsafe fn from_foreign (ptr: *const core::ffi::c_void ) ->Self ; + +/// Tries to convert a foreign-owned object back to a Rust-owned one. +/// +/// A convenience wrapper over [`from_foreign`] that returns [`None`] +/// if `ptr` is null. +/// +/// # Safety +/// +/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for +/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet. +/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for +/// this object must have been dropped. +unsafe fn try_from_foreign (ptr: *const core::ffi::c_void ) ->Option <Self > { +if ptr.is_null () { +None + }else { +Some (Self ::from_foreign (ptr)) + } + } }impl <T : 'static>ForeignOwnable for Box <T > {
Signature · 1 line
2.39.2
Hi Obei,
Some extra nits below that you can fix for v3 (since a v3 will be needed anyway due to the compilation error reported in Zulip).
Quoted 3 lines · Obei Sideg
Currently
ForeignOwnable::from_foreign()only works for non-null pointers for the existing impls (e.g. Box, Arc). It may create a few
Markdown may be used here for Box and Arc since you use it for the other.
Quoted 10 lines
duplicate code like:
```rust
//pis a maybe null pointer
ifp.is_null(){
None
} else {
Some(unsafe { T::from_foreign(p) })
}
``
Triple-backquote here.
Also please do the usual indentation for the code.
Quoted 2 lines
No newline between the tags.
Quoted 3 lines
Cc Alice Ryhl <[email protected]>Cc Matt Gilbride <[email protected]>Cc Miguel Ojeda <[email protected]>
Normally you don't need to add a Cc tag with the maintainer that is supposed to pick the patch.
For Alice and Matt, did you have a particular reason to explicitly list them?
Quoted 1 line
Reviewed-by Trevor Gross <[email protected]>
Since the patch changed substantially, you should not have picked the Reviewed-by, i.e. he did not approve this patch (in particular, he mentioned this in Zulip himself, so... :)
Also, please note that you should indicate v2/v3/... in the title
(please see https://docs.kernel.org/process/submitting-patches.html#the-canonical-patch-format).
Finally, I think Alice's comment should be applied, i.e. the precondition on ptr only applies if ptr is non-null for this function, so I think we should add it.
Thanks!
Cheers,
Miguel