RetroFed Forum
Your home away from modern life.
Announcing Rust 1.99.0 | Rust Blog
Post Reply
AuthorMessage
SorteKanin feddit.dk SorteKanin@feddit.dk
Joined: 2023-04-27
Quote

35   Original
arendjr programming.dev arendjr@programming.dev
Joined: 2024-02-26
#2 · Quote Reply

Vec::into_parts() returns a NonNull pointer, but isn’t the pointer allowed to be null if the vector is empty? I would expect that this method should be able to return a null pointer, but I’m probably missing something…

2
SorteKanin feddit.dk SorteKanin@feddit.dk
Joined: 2023-04-27
#3 · (edited) Quote Reply

Vec::into_parts() returns a NonNull pointer, but isn’t the pointer allowed to be null if the vector is empty? I would expect that this method should be able to return a null pointer, but I’m probably missing something…

It's a good question. I believe Vec uses a near-zero pointer but not actually a null pointer for an empty Vec. I think part of the reason is that it makes for better optimisation of Option<Vec> which can then have the same size as Vec with the pointer being null signifying None.

Another big reason is that references are guaranteed not to be null and you can get a &[T] from an empty vec and that reference can't be null. Even in a non empty vec you need to consider this if T is zero sized.

3
arendjr programming.dev arendjr@programming.dev
Joined: 2024-02-26
#4 · Quote Reply

It's a good question. I believe Vec uses a near-zero pointer but not actually a null pointer for an empty Vec. I think part of the reason is that it makes for better optimisation of Option&lt;Vec&gt; which can then have the same size as Vec with the pointer being null signifying None. Another big reason is that references are guaranteed not to be null and you can get a &amp;[T] from an empty ve...

Ah yes, that makes sense. Curious what it points to then when empty…

2
SorteKanin feddit.dk SorteKanin@feddit.dk
Joined: 2023-04-27
#5 · Quote Reply

Ah yes, that makes sense. Curious what it points to then when empty…

I looked it up, it's a pointer equal to the alignment of the type. So usually some small power of two.

4
ExperimentalGuy programming.dev ExperimentalGuy@programming.dev
Joined: 2023-07-25
#6 · Quote Reply

Stable variadic functions is cool AF!

6
fil programming.dev fil@programming.dev
Joined: 2023-08-29
#7 · (edited) Quote Reply

Ah yes, that makes sense. Curious what it points to then when empty…

Vec::<u8>::new().as_ptr() = 0x0000000000000001

playground

2
TehPers beehaw.org TehPers@beehaw.org
Joined: 2023-06-21
#8 · (edited) Quote Reply

The extern variadic functions are pretty cool. What I'm actually most excited about is what just moved to beta, though. In particular, stabilization of both Allocator and ! are now in beta.

3
BB_C programming.dev BB_C@programming.dev
Joined: 2023-07-06
#9 · Quote Reply

Vec::into_parts() returns a NonNull pointer, but isn’t the pointer allowed to be null if the vector is empty? I would expect that this method should be able to return a null pointer, but I’m probably missing something…

To add to the other answers. Usually, the easiest way to answer such questions is to click on source. Then in this case, you would immediately see this comment:

// SAFETY: A `Vec` always has a non-null pointer.
2
Post Reply
Back to top