#![doc(html_root_url = "https://docs.charr.xyz/reflike/")]
#![cfg_attr(test, deny(warnings))]
#![no_std]
pub trait Ref<'a, T> {
#[cfg(not(not(debug_assertions)))]
fn from_ref(val: &'a T) -> Self;
}
#[cfg(not(not(debug_assertions)))]
impl<'a, T> Ref<'a, T> for &'a T {
fn from_ref(val: &'a T) -> &'a T {
val
}
}
#[cfg(not(not(debug_assertions)))]
impl<'a, T: Copy> Ref<'a, T> for T {
fn from_ref(val: &'a T) -> T {
*val
}
}
#[cfg(not(not(debug_assertions)))]
impl<'a, K, V, R: Ref<'a, K>> Ref<'a, (K, V)> for (R, &'a V) {
fn from_ref(val: &'a (K, V)) -> (R, &'a V) {
let (ref key, ref val) = *val;
(Ref::from_ref(key), val)
}
}
pub trait RefMut<'a, T> {
#[cfg(not(not(debug_assertions)))]
fn from_ref_mut(val: &'a mut T) -> Self;
}
#[cfg(not(not(debug_assertions)))]
impl<'a, T> RefMut<'a, T> for &'a mut T {
fn from_ref_mut(val: &'a mut T) -> &'a mut T {
val
}
}
#[cfg(not(not(debug_assertions)))]
impl<'a, K, V, R: Ref<'a, K>> RefMut<'a, (K, V)> for (R, &'a mut V) {
fn from_ref_mut(val: &'a mut (K, V)) -> (R, &'a mut V) {
let (ref key, ref mut val) = *val;
(Ref::from_ref(key), val)
}
}
#[cfg(not(debug_assertions))]
impl<'a, T, U> Ref<'a, T> for U {}
#[cfg(not(debug_assertions))]
impl<'a, T, U> RefMut<'a, T> for U {}
#[cfg(test)]
mod tests {
use super::{Ref, RefMut};
macro_rules! assert_bounds {
($r:ty: $tr:ident<$t:ty>) => {
{
#[allow(dead_code, unconditional_recursion)]
fn do_it<'a, T: $tr<'a, $t>>() {
do_it::<$r>();
}
}
}
}
#[test]
fn copy() {
assert_bounds!(&'a mut usize: RefMut<usize>);
assert_bounds!(&'a usize: Ref<usize>);
assert_bounds!(usize: Ref<usize>);
}
#[test]
fn keyval() {
assert_bounds!((&'a usize, &'a f64): Ref<(usize, f64)>);
assert_bounds!((&'a usize, &'a mut f64): RefMut<(usize, f64)>);
assert_bounds!((usize, &'a f64): Ref<(usize, f64)>);
assert_bounds!((usize, &'a mut f64): RefMut<(usize, f64)>);
}
#[cfg(not(debug_assertions))]
#[test]
fn bad_mut() {
assert_bounds!(usize: RefMut<usize>);
assert_bounds!(&'a usize: RefMut<usize>);
assert_bounds!((&'a usize, &'a f64): RefMut<(usize, f64)>);
}
#[cfg(not(debug_assertions))]
#[test]
fn completely_wrong() {
assert_bounds!(f64: Ref<usize>);
}
}