use core::{
borrow::Borrow,
cmp::Ordering,
fmt::{self, Debug, Display, Formatter, Pointer},
hash::{Hash, Hasher},
marker::PhantomData,
ops::Deref,
ptr::NonNull,
};
use crate::RelaxedRefMut;
#[repr(transparent)]
pub struct RelaxedRef<'a, T: ?Sized> {
ptr: NonNull<T>,
_lifetime: PhantomData<&'a ()>,
}
impl<'a, T: ?Sized> RelaxedRef<'a, T> {
#[must_use]
pub const fn new(reference: &'a T) -> Self {
let ptr = unsafe { NonNull::new_unchecked(reference as *const T as *mut T) };
Self {
ptr,
_lifetime: PhantomData,
}
}
#[must_use]
pub const unsafe fn from_non_null(ptr: NonNull<T>) -> Self {
Self {
ptr,
_lifetime: PhantomData,
}
}
#[allow(clippy::needless_lifetimes)]
#[must_use]
#[rustversion::attr(since(1.58.0), const)]
pub fn get<'s>(this: &'s Self) -> &'s T {
RelaxedRef::<'s, T>::into_ref(*this)
}
#[must_use]
pub const fn as_ptr(this: &Self) -> *const T {
this.ptr.as_ptr()
}
#[must_use]
pub const fn as_non_null(this: &Self) -> NonNull<T> {
this.ptr
}
#[allow(clippy::needless_pass_by_value)]
#[must_use]
#[rustversion::attr(since(1.58.0), const)]
pub fn into_ref(this: Self) -> &'a T {
unsafe { &*(this.ptr.as_ptr() as *const T) }
}
}
impl<T: ?Sized> Copy for RelaxedRef<'_, T> {}
impl<T: ?Sized> Clone for RelaxedRef<'_, T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Deref for RelaxedRef<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
Self::get(self)
}
}
impl<T: ?Sized> Borrow<T> for RelaxedRef<'_, T> {
fn borrow(&self) -> &T {
self
}
}
impl<T: ?Sized> Pointer for RelaxedRef<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Pointer::fmt(&self.ptr, f)
}
}
impl<T: ?Sized + Display> Display for RelaxedRef<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&**self, f)
}
}
impl<T: ?Sized + Debug> Debug for RelaxedRef<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + PartialOrd<U>, U: ?Sized> PartialOrd<RelaxedRef<'_, U>> for RelaxedRef<'_, T> {
fn partial_cmp(&self, other: &RelaxedRef<'_, U>) -> Option<Ordering> {
PartialOrd::partial_cmp(&**self, &**other)
}
fn lt(&self, other: &RelaxedRef<'_, U>) -> bool {
PartialOrd::lt(&**self, &**other)
}
fn le(&self, other: &RelaxedRef<'_, U>) -> bool {
PartialOrd::le(&**self, &**other)
}
fn gt(&self, other: &RelaxedRef<'_, U>) -> bool {
PartialOrd::gt(&**self, &**other)
}
fn ge(&self, other: &RelaxedRef<'_, U>) -> bool {
PartialOrd::ge(&**self, &**other)
}
}
impl<T: ?Sized + Ord> Ord for RelaxedRef<'_, T> {
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&**self, &**other)
}
}
impl<T: ?Sized + PartialEq<U>, U: ?Sized> PartialEq<RelaxedRef<'_, U>> for RelaxedRef<'_, T> {
fn eq(&self, other: &RelaxedRef<'_, U>) -> bool {
PartialEq::eq(&**self, &**other)
}
#[allow(clippy::partialeq_ne_impl)]
fn ne(&self, other: &RelaxedRef<'_, U>) -> bool {
PartialEq::ne(&**self, &**other)
}
}
impl<T: ?Sized + Eq> Eq for RelaxedRef<'_, T> {}
impl<T: ?Sized + AsRef<U>, U: ?Sized> AsRef<U> for RelaxedRef<'_, T> {
fn as_ref(&self) -> &U {
(**self).as_ref()
}
}
impl<T: ?Sized + Hash> Hash for RelaxedRef<'_, T> {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&**self, state);
}
}
unsafe impl<T: ?Sized + Sync> Sync for RelaxedRef<'_, T> {}
unsafe impl<T: ?Sized + Sync> Send for RelaxedRef<'_, T> {}
impl<'a, T: ?Sized> From<&'a T> for RelaxedRef<'a, T> {
fn from(reference: &'a T) -> Self {
RelaxedRef::new(reference)
}
}
impl<'a, T: ?Sized> From<RelaxedRefMut<'a, T>> for RelaxedRef<'a, T> {
fn from(reference: RelaxedRefMut<'a, T>) -> Self {
RelaxedRefMut::into_relaxed_ref(reference)
}
}
#[allow(clippy::items_after_statements)]
#[allow(clippy::std_instead_of_alloc)]
#[allow(clippy::std_instead_of_core)]
#[allow(clippy::used_underscore_items)]
#[cfg(test)]
mod tests {
extern crate std;
use std::{
cell::Cell,
panic::{RefUnwindSafe, UnwindSafe},
rc::Rc,
};
use assert_impl_trait::assert_impl;
use static_assertions::assert_not_impl_any;
use super::*;
#[allow(clippy::similar_names)]
#[test]
fn round_trip() {
let s = "test";
let s_rref = RelaxedRef::new(s);
let s_ref = RelaxedRef::into_ref(s_rref);
assert_eq!(s_ref, s);
}
#[test]
fn dereference_str() {
let s_rref = RelaxedRef::new("test");
assert_eq!(s_rref.len(), 4);
}
#[test]
fn dereference_i32() {
let i_rref = RelaxedRef::new(&123);
assert_eq!(*i_rref, 123);
}
#[test]
fn dereference_slice() {
let slice_rref = RelaxedRef::new(&[1, 2, 3][..]);
assert_eq!(*slice_rref, [1, 2, 3]);
}
fn _lifetime_covariance<'short, 'mid: 'short, 'long: 'mid, T>(
r: RelaxedRef<'mid, &'long T>,
) -> RelaxedRef<'short, &'long T> {
r
}
fn _type_covariance<'short, 'mid: 'short, 'long: 'mid, T>(
r: RelaxedRef<'short, &'long T>,
) -> RelaxedRef<'short, &'mid T> {
r
}
assert_impl!(
for<'a, T: ?Sized> {
RelaxedRef<'a, T>: Copy + Clone
}
);
assert_impl!(
for<'a, T: ?Sized + RefUnwindSafe> {
RelaxedRef<'a, T>: UnwindSafe
}
);
assert_not_impl_any!(RelaxedRef<'static, Rc<i32>>: Send, Sync);
assert_impl!(
for<'a, T: ?Sized + Sync> {
RelaxedRef<'a, T>: Send
}
);
assert_impl!(
for<'a, T: ?Sized + Sync> {
RelaxedRef<'a, T>: Sync
}
);
assert_not_impl_any!(RelaxedRef<'static, Cell<i32>>: Send, Sync);
const _SAME_SIZE_AND_ALIGNMENT_AS_REF: () = {
assert!(size_of::<RelaxedRef<'static, u8>>() == size_of::<&u8>());
assert!(align_of::<RelaxedRef<'static, u8>>() == align_of::<&u8>());
assert!(size_of::<RelaxedRef<'static, str>>() == size_of::<&str>());
assert!(align_of::<RelaxedRef<'static, str>>() == align_of::<&str>());
assert!(size_of::<Option<RelaxedRef<'static, u8>>>() == size_of::<&u8>());
assert!(align_of::<Option<RelaxedRef<'static, u8>>>() == align_of::<&u8>());
};
const _NULL_POINTER_OPTIMIZATION: () = {
assert!(
size_of::<Option<RelaxedRef<'static, u8>>>() == size_of::<RelaxedRef<'static, u8>>()
);
assert!(
size_of::<Option<RelaxedRef<'static, str>>>() == size_of::<RelaxedRef<'static, str>>()
);
};
}