Skip to content
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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Voronar
Copy link

@Voronar Voronar commented Jan 26, 2022

No description provided.

Copy link
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.

src/en/07_ffi.md Show resolved Hide resolved
Co-authored-by: Daniel Henry-Mantilla <[email protected]>
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.

2 participants