Skip to content

Makes FFI-MEM-WRAPPING example a bit more Rusty#56

Open
Voronar wants to merge 2 commits intoANSSI-FR:masterfrom
Voronar:ffi_null_check_rusty_way
Open

Makes FFI-MEM-WRAPPING example a bit more Rusty#56
Voronar wants to merge 2 commits intoANSSI-FR:masterfrom
Voronar:ffi_null_check_rusty_way

Conversation

@Voronar
Copy link
Copy Markdown

@Voronar Voronar commented Jan 26, 2022

No description provided.

Copy link
Copy Markdown
Contributor

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

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

I personally do like the changes; I'd kind of suggest that ptr::NonNull<Foo> be used instead of a through-&mut raw pointer, because the semantics w.r.t. aliasing are not exactly the same, and that when aliasing is involved things would be a bit more subtle. Raw pointers are better at dodging aliasing assumptions with the compiler:

- pub struct Foo(*mut RawFoo);
+ pub struct Foo(ptr::NonNull<RawFoo>);

  …

  impl Foo {
      /// Create a Foo
      pub fn new() -> Option<Foo> {
          // The `?` operator here ensures `None` is returned on `NULL`.
-         let raw_ptr = unsafe { foo_create().as_mut()? };
+         let raw_ptr = ptr::NonNull::new(unsafe { foo_create() })?;

etc.

Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
@polazarus
Copy link
Copy Markdown
Contributor

I think NonNull was not stable at the time of writing ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants