#![no_std]
extern crate maybe_std as base;
use base::borrow::{Borrow, BorrowMut};
use base::ops::{Deref, DerefMut};
use base::fmt::Pointer;
pub trait SmartPointer<T: ?Sized>: Sized + AsRef<T> + Borrow<T> + Deref<Target = T> + Pointer
{
fn new(t: T) -> Self where T: Sized;
fn try_unwrap(this: Self) -> Result<T, Self> where T: Sized;
fn ptr_eq(a: &Self, b: &Self) -> bool {
base::ptr::eq(a.borrow(), b.borrow())
}
}
pub trait SmartPointerMut<T: ?Sized>: SmartPointer<T> + AsMut<T> + BorrowMut<T> + DerefMut<Target = T> {}
pub trait IntoMut<T: ?Sized>: SmartPointer<T> {
type MutablePointer: SmartPointerMut<T> + Into<Self>;
fn can_make_mut(this: &Self) -> bool;
unsafe fn into_mut_unchecked(this: Self) -> Self::MutablePointer;
fn into_mut(this: Self) -> Result<Self::MutablePointer, Self> {
if IntoMut::can_make_mut(&this) {
Ok(unsafe { IntoMut::into_mut_unchecked(this) })
} else {
Err(this)
}
}
unsafe fn get_mut_unchecked(this: &Self) -> &mut T;
fn get_mut(this: &Self) -> Option<&mut T> {
if IntoMut::can_make_mut(this) {
Some(unsafe { IntoMut::get_mut_unchecked(this) })
} else {
None
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}