PtrOption and PtrResult

Would there be any appetite to add an Option and Result types specialised for pointers? It's very common for C functions to use null to mean "no value" in function parameters or "error" in returned values.

If I understand std::ptr::NonNull documentation correctly this can be done without needing anything new in the standard library. E.g. something like this:

use core::ptr;
type PtrOption<T> = Option<ptr::NonNull<T>>;
type PtrResult<T> = Result<ptr::NonNull<T>, ()>;

However, I think there's merit in having a consistently named type that's conveniently available in core. Especially given that the NonNull documentation doesn't seem to encourage its use.


(Btw, sorry if this has been brought up before. I did search issues and previous discussions but there was a lot and nothing seemed obviously relevant)

Rust is build on compositions of types. So unless there is functional gains to adding such a type, this is generally avoided. Another place where this can be seen is the lack of a type describing Arc<Mutex<T>>.

Further more, type aliases in std are very rare, as they mostly arise from backwards compatibility.

I think it would be more appropriate to improve documentation of ffi to more clearly communicate relevant type compositions, such as Option<fn>.

9 Likes

I see, so this would be considered a documentation issue. If anyone else is interested, there is an open issue for improving the docs for NonNull but it doesn't seem to have had a lot of attention.

The Rust book is quite spartan when it comes to FFI.

The nomicon does document nullable pointer optimizations in an FFI context but not NonNull specifically. It does however describe using Option<fn>.

2 Likes