diff --git a/src/lib.rs b/src/lib.rs index edf66c5..c834d0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,6 +117,8 @@ //! [`unsize_stable`]: Vec::unsize_stable #![cfg_attr(feature = "unstable", feature(coerce_unsized))] +#![cfg_attr(feature = "unstable", feature(set_ptr_value))] +#![cfg_attr(feature = "unstable", feature(pointer_byte_offsets))] #![warn(missing_docs)] #![warn(clippy::pedantic)] #![warn(clippy::nursery)] diff --git a/src/ptr_ext.rs b/src/ptr_ext.rs index 50ba933..2eddc1d 100644 --- a/src/ptr_ext.rs +++ b/src/ptr_ext.rs @@ -1,9 +1,11 @@ use std::{ alloc::{alloc, Layout}, mem::size_of_val, - ptr::addr_of_mut, }; +#[cfg(not(feature = "unstable"))] +use std::ptr::addr_of_mut; + use self::__priv::Sealed; mod __priv { @@ -52,10 +54,18 @@ impl PtrExt for *const T { self.with_addr(addr.addr()) } - fn with_addr(mut self, addr: usize) -> Self { - // TODO: aaaaand this is cheating. sorry yall, cant do this until `set_ptr_value` lands - unsafe { *addr_of_mut!(self).cast() = addr } - self + fn with_addr(self, addr: usize) -> Self { + #[cfg(feature = "unstable")] + { + self.wrapping_byte_sub(self.addr()).wrapping_byte_add(addr) + } + #[cfg(not(feature = "unstable"))] + { + let mut this = self; + // TODO: aaaaand this is cheating. sorry yall, cant do this until `set_ptr_value` lands + unsafe { *addr_of_mut!(this).cast() = addr } + this + } } unsafe fn get_end(self) -> Self { @@ -103,10 +113,18 @@ impl PtrExt for *mut T { self.with_addr(addr.addr()) } - fn with_addr(mut self, addr: usize) -> Self { - // TODO: aaaaand this is cheating. sorry yall, cant do this until `set_ptr_value` lands - unsafe { *addr_of_mut!(self).cast() = addr } - self + fn with_addr(self, addr: usize) -> Self { + #[cfg(feature = "unstable")] + { + self.wrapping_byte_sub(self.addr()).wrapping_byte_add(addr) + } + #[cfg(not(feature = "unstable"))] + { + let mut this = self; + // TODO: aaaaand this is cheating. sorry yall, cant do this until `set_ptr_value` lands + unsafe { *addr_of_mut!(this).cast() = addr } + this + } } unsafe fn get_end(self) -> Self { @@ -156,7 +174,14 @@ impl ConstPtrExt for *const T { // } fn with_meta_from(self, ptr: *const U) -> *const U { - ptr.with_addr_from(self) + #[cfg(feature = "unstable")] + { + self.with_metadata_of(ptr) + } + #[cfg(not(feature = "unstable"))] + { + ptr.with_addr_from(self) + } } } @@ -179,6 +204,13 @@ impl MutPtrExt for *mut T { // } fn with_meta_from(self, ptr: *const U) -> *mut U { - ptr.with_addr_from(self) as _ + #[cfg(feature = "unstable")] + { + self.with_metadata_of(ptr as _) + } + #[cfg(not(feature = "unstable"))] + { + ptr.with_addr_from(self) as _ + } } } diff --git a/src/test.rs b/src/test.rs index b184504..dadcb06 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,4 +1,4 @@ -//! Always use `cargo miri test`, and never just `cargo test`. +//! Always use `cargo miri test --all-features`, and never just `cargo test`. use rand::{thread_rng, Rng};