I’m in the midst of designing an API around raw pointers. I want to follow the example of the stdlib as much as possible. I want to know how to name the method(s) that extract a raw pointer out of a container. It looks like there are two different ways it’s done:
- Provide a single method,
as_ptr() -> *mut T. The caller can cast/coerce to
*const Tif necessary.
- Provide both
as_ptr() -> *const Tand
as_mut_ptr() -> *mut T.
Here are the 7 instances of
as_ptr() I found in the stdlib. Some return
*mut T. Others return
*const T and are paired with a matching
as_mut_ptr(), except for
CStr which is asymmetrical.
slice::as_ptr() -------> *const T
str::as_ptr() ---------> *const u8
Cell::as_ptr() --------> *mut T
RefCell::as_ptr() -----> *mut T
CStr::as_ptr() --------> *const c_char
MaybeUninit::as_ptr() -> *const T
NonNull::as_ptr() -----> *mut T
Unfortunately, 4 of these return
*const T and 3 return
*mut T. It’s as close as you can get to a 50/50 split.
What’s the rationale here? Is this just an unfortunate historical inconsistency, or is there some common factor between the two groups that I’m not seeing, that should lead me to choose one approach over the other?