use alux_ext::ext;
#[ext(name = IntoExt)]
pub impl<This> This {
fn to<R>(self) -> R
where
This: Into<R>,
{
self.into()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fmt::Debug;
use std::sync::Arc;
use mockall::mock;
use mockall::predicate::*;
mock! {
FromSt<T: 'static> {}
impl<T: 'static> From<T> for FromSt<T> {
fn from(t: T) -> Self;
}
}
fn proof_to_calls_from<T: Debug + Clone + PartialEq + Send + 'static>(t: T) {
let ctx = MockFromSt::<T>::from_context();
ctx.expect()
.times(1)
.with(eq(t.clone()))
.returning(|_| MockFromSt::default());
t.to::<MockFromSt<T>>();
}
#[test]
fn run_proof_to_calls_from() {
proof_to_calls_from(42);
proof_to_calls_from("Hello");
proof_to_calls_from(());
proof_to_calls_from(Arc::new(42));
}
use proptest::prelude::*;
#[derive(Debug, PartialEq)]
struct Wrap<T>(T);
impl<T> From<T> for Wrap<T> {
fn from(t: T) -> Self {
Wrap(t)
}
}
fn from_and_to_fun_equal<T: PartialEq + Debug + Clone>(t: T) {
let wrapped_expected: Wrap<T> = From::from(t.clone());
let wrapped_actual = t.to::<Wrap<T>>();
assert_eq!(wrapped_expected, wrapped_actual);
}
proptest! {
#[test]
fn from_and_to_fun_equal_int(v: i128) {
from_and_to_fun_equal(v);
}
#[test]
fn from_and_to_fun_equal_str(v: String) {
from_and_to_fun_equal(v);
}
}
}