Skip to content

Please consider clarifying use of unsafe in README example #433

Open
@grothesque

Description

@grothesque

Thanks for providing this amazing library!

The code example given in this project's README file demonstrates the use of PyO3. I guess that it should also serve as a pedagogical model of how PyO3 should be used. After all, this is often going to be the first bit of PyO3-using code that prospective users are going to see.

Now that example features a short unsafe block without any explanation. But the usage of unsafe in Rust code means that the safety of the featured block has been carefully verified and the compiler should trust that.

In practice the usage of unsafe is quite often accompanied by a comment that explains why it is safe. This should certainly be the case in pedagogical code. I think that such a comment would be very helpful here.

Perhaps this particular use of unsafe is obvious to seasoned PyO3 users, but it certainly isn't to newbies for whom this code will often be the first contact with PyO3. Here are some question answers that may come up:

  • Why is this particular use of unsafe safe after all?
  • Are there any assumptions without which this would no longer be the case?
  • The first function of the module takes PyReadonlyArrayDyn<'py, f64> arguments. It seems that the second function could similarly take a PyReadwriteArrayDyn<'py, f64> argument (which provides a safe as_array_mut method), but it takes &Bound<'py, PyArrayDyn<f64>> instead. Is this indeed a choice and what is the reason behind it?

I'm not suggesting that a long discussion of this issue should be added to the README. Probably a short comment and a few pointers into the documentation would be enough.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions