Refactor set_ptr_value as with_metadata_of by 197g · Pull Request #95249 · rust-lang/rust (original) (raw)

Replaces set_ptr_value (#75091) with methods of reversed argument order:

impl<T: ?Sized> *mut T { pub fn with_metadata_of<U: ?Sized>(self, val: *mut U) -> *mut U; }

impl<T: ?Sized> *const T { pub fn with_metadata_of<U: ?Sized>(self, val: *const U) -> *const U; }

By reversing the arguments we achieve several clarifications:

Hopefully the usage examples in sync/rc.rs serve as sufficient examples of the style to convince the reader of the readability improvements of this style, when compared to the previous order of arguments.

I want to take the opportunity to motivate inclusion of this method separate from metadata API, separate from feature(ptr_metadata). It does not involve the Pointee trait in any form. This may be regarded as a very, very light form that does not commit to any details of the pointee trait, or its associated metadata. There are several use cases for which this is already sufficient and no further inspection of metadata is necessary.