1

I'm calling a C constructor function that allocates memory returns one pointer.

I found some similar questions. they use Nonnull or *mut T to wrap it.

And I also found another similar structure Unique, it will take the ownership of T.

This makes me wonder, what is the difference between them and how should I choose?

2
  • Is there anything not clarified by the documentation of NonNull? Unique is a private type in the standard library, not meant to be used directly. Commented Jun 17, 2022 at 9:52
  • @E_net4-MrDownvoter I'm too stupid to notice that Unique is an internally used struct. Thanks for your answer~ Commented Jun 17, 2022 at 11:02

1 Answer 1

3

Use NonNull if your pointer is never null. Use *const T or *mut T otherwise.

Unique is a private pointer for the standard library, you cannot use it (I think it was used to be exposed unstably, but not anymore). It represents an owned type: for example, it is used for Box and Vec. It is basically NonNull, although it has some differences because it is considered owned: it impls Send/Sync (and other auto traits) if T does, and maybe it should be considered owned for the aliasing model and Miri (although it is not obvious we want that, and currently it is not).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.