61 lines
1.3 KiB
Rust
61 lines
1.3 KiB
Rust
#[derive(Clone, Debug)]
|
|
struct Context {
|
|
id: i32,
|
|
param: String,
|
|
}
|
|
|
|
trait FromSourceRef<Source> {
|
|
fn from_source_ref(source: &Source) -> Self;
|
|
}
|
|
|
|
struct Id(pub i32);
|
|
impl FromSourceRef<Context> for Id {
|
|
fn from_source_ref(context: &Context) -> Self {
|
|
Id(context.id)
|
|
}
|
|
}
|
|
|
|
struct Param(pub String);
|
|
impl FromSourceRef<Context> for Param {
|
|
fn from_source_ref(context: &Context) -> Self {
|
|
Param(context.param.clone())
|
|
}
|
|
}
|
|
|
|
fn print_id(Id(id): Id) {
|
|
println!("id is {id}");
|
|
}
|
|
|
|
fn print_all(Param(param): Param, Id(id): Id) {
|
|
println!("id is {id} and param is {param}")
|
|
}
|
|
|
|
trait MagicFunction<T, C> {
|
|
fn call(self, context: C);
|
|
}
|
|
|
|
impl<F: Fn(T), T: FromSourceRef<C>, C> MagicFunction<(T,), C> for F {
|
|
fn call(self, context: C) {
|
|
(self)(T::from_source_ref(&context))
|
|
}
|
|
}
|
|
impl<F: Fn(T1, T2), T1: FromSourceRef<C>, T2: FromSourceRef<C>, C> MagicFunction<(T1, T2), C> for F {
|
|
fn call(self, context: C) {
|
|
(self)(T1::from_source_ref(&context), T2::from_source_ref(&context))
|
|
}
|
|
}
|
|
|
|
fn trigger<T, C>(function: impl MagicFunction<T, C>, context: C) {
|
|
function.call(context)
|
|
}
|
|
|
|
fn main() {
|
|
let context = Context {
|
|
id: 82137,
|
|
param: "bruh".into(),
|
|
};
|
|
|
|
trigger(print_id, context.clone());
|
|
trigger(print_all, context.clone());
|
|
}
|