From: Ian Jackson Date: Thu, 20 Apr 2023 15:45:24 +0000 (+0100) Subject: w X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=300c1e1a4a0cd88d70ede40d522a8720a4ffb14c;p=rust-experiments.git w --- diff --git a/src/main.rs b/src/main.rs index 9860f70..3b6259c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,36 +1,72 @@ use std::any::*; use std::sync::*; +pub trait Callable: Any { + type Output; + fn callable_call(self, a: Args) -> Self::Output; +} +impl Callable<(A,)> for F where F: Fn(A) -> O + 'static { + type Output = O; + fn callable_call(self, (a,): (A,)) -> O { (self)(a) } +} +impl Callable<(A,B)> for F where F: Fn(A,B) -> O + 'static { + type Output = O; + fn callable_call(self, (a,b): (A,B)) -> O { (self)(a,b) } +} + pub trait Object: Any + Send + Sync {} impl Object for String {} -pub trait MI { - fn o_ti(&self) -> TypeId; - fn call(&self, o: Arc) -> usize; +pub trait MI: Any { + fn o_ti(&self) -> TypeId { TypeId::of::() } + fn call(&self, o: Arc, u: ()) -> usize; } +/* impl MI for fn(Arc) -> usize { fn o_ti(&self) -> TypeId { TypeId::of::() } fn call(&self, o: Arc) -> usize { (self)(Arc::downcast(o).unwrap()) } -} +}*/ fn example(o: Arc) -> usize { o.len() } -const fn k>(f: &F) -> &dyn MI + '_ { +/* +struct K(F); + +const fn k>(f: F) -> K { + f +} + +&dyn MI { move |o| f(o) - /* struct TE(&F); impl MI for TE { fn o_ti(&self) -> TypeId { TypeId::of::() } fn call(&self, o: Arc) -> usize { } - }*/ + } } + */ +/* +struct Thunk(C); +impl MI for Thunk +where C: Fn((O,)) -> usize +{ + fn call(&self, o: Arc, u: ()) -> usize { + (self)((Arc::downcast(o).unwrap(),)) + } +} +*/ fn main() { - let v: &dyn MI = k(&example); + const CL: () = { + |t| Callable::callable_call(example, t); + () + }; +//example.callable_call(); +// let v: &dyn MI = k(example); println!("Hello, world!"); }