meta: initial commit (archival)
This commit is contained in:
199
src/bin/magical-handler-functions.rs
Normal file
199
src/bin/magical-handler-functions.rs
Normal file
@@ -0,0 +1,199 @@
|
||||
mod fundamentals {
|
||||
pub struct FromSourceRefMarker;
|
||||
pub struct FromSourceMarker;
|
||||
pub trait FromMarker {} // TODO: I don't want to pub this..
|
||||
impl FromMarker for FromSourceRefMarker {}
|
||||
impl FromMarker for FromSourceMarker {}
|
||||
|
||||
pub trait FromSourceRef<Source> {
|
||||
fn from_source_ref(source: &Source) -> Self;
|
||||
}
|
||||
pub trait FromSource<Source> {
|
||||
fn from_source(source: Source) -> Self;
|
||||
}
|
||||
|
||||
pub trait IntoHandler<Extractors, Return, Source, M>
|
||||
where
|
||||
M: FromMarker,
|
||||
{
|
||||
fn call(self, source: Source) -> Return;
|
||||
}
|
||||
|
||||
// impl<F, Return, Source, Marker> IntoHandler<(), Return, Source, Marker> for F
|
||||
// where
|
||||
// F: Fn() -> Return,
|
||||
// Marker: FromMarker,
|
||||
// {
|
||||
// fn call(self, _source: Source) -> Return {
|
||||
// (self)()
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<F, Return, Source> IntoHandler<(), Return, Source, FromSourceRefMarker> for F
|
||||
where
|
||||
F: Fn() -> Return,
|
||||
{
|
||||
fn call(self, _source: Source) -> Return {
|
||||
(self)()
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T1, Return, Source> IntoHandler<(T1,), Return, Source, FromSourceRefMarker> for F
|
||||
where
|
||||
F: Fn(T1) -> Return,
|
||||
T1: FromSourceRef<Source>,
|
||||
{
|
||||
fn call(self, source: Source) -> Return {
|
||||
(self)(T1::from_source_ref(&source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T1, Return, Source> IntoHandler<(T1,), Return, Source, FromSourceMarker> for F
|
||||
where
|
||||
F: Fn(T1) -> Return,
|
||||
T1: FromSource<Source>,
|
||||
{
|
||||
fn call(self, source: Source) -> Return {
|
||||
(self)(T1::from_source(source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T1, T2, Return, Source> IntoHandler<(T1, T2), Return, Source, FromSourceRefMarker> for F
|
||||
where
|
||||
F: Fn(T1, T2) -> Return,
|
||||
T1: FromSourceRef<Source>,
|
||||
T2: FromSourceRef<Source>,
|
||||
{
|
||||
fn call(self, source: Source) -> Return {
|
||||
(self)(T1::from_source_ref(&source), T2::from_source_ref(&source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T1, T2, Return, Source> IntoHandler<(T1, T2), Return, Source, FromSourceMarker> for F
|
||||
where
|
||||
F: Fn(T1, T2) -> Return,
|
||||
T1: FromSourceRef<Source>,
|
||||
T2: FromSource<Source>,
|
||||
{
|
||||
fn call(self, source: Source) -> Return {
|
||||
(self)(T1::from_source_ref(&source), T2::from_source(source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T1, T2, T3, Return, Source>
|
||||
IntoHandler<(T1, T2, T3), Return, Source, FromSourceRefMarker> for F
|
||||
where
|
||||
F: Fn(T1, T2, T3) -> Return,
|
||||
T1: FromSourceRef<Source>,
|
||||
T2: FromSourceRef<Source>,
|
||||
T3: FromSourceRef<Source>,
|
||||
{
|
||||
fn call(self, source: Source) -> Return {
|
||||
(self)(
|
||||
T1::from_source_ref(&source),
|
||||
T2::from_source_ref(&source),
|
||||
T3::from_source_ref(&source),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T1, T2, T3, Return, Source> IntoHandler<(T1, T2, T3), Return, Source, FromSourceMarker>
|
||||
for F
|
||||
where
|
||||
F: Fn(T1, T2, T3) -> Return,
|
||||
T1: FromSourceRef<Source>,
|
||||
T2: FromSourceRef<Source>,
|
||||
T3: FromSource<Source>,
|
||||
{
|
||||
fn call(self, source: Source) -> Return {
|
||||
(self)(
|
||||
T1::from_source_ref(&source),
|
||||
T2::from_source_ref(&source),
|
||||
T3::from_source(source),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod library {
|
||||
use super::fundamentals::*;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Request {
|
||||
customer: String,
|
||||
order_id: u64,
|
||||
amount_spent: f64,
|
||||
}
|
||||
impl FromSource<Request> for Request {
|
||||
fn from_source(source: Request) -> Self {
|
||||
source
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Customer(pub String);
|
||||
impl FromSource<Request> for Customer {
|
||||
fn from_source(source: Request) -> Self {
|
||||
Self(source.customer)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OrderId(pub u64);
|
||||
impl FromSourceRef<Request> for OrderId {
|
||||
fn from_source_ref(source: &Request) -> Self {
|
||||
Self(source.order_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AmountSpent(pub f64);
|
||||
impl FromSourceRef<Request> for AmountSpent {
|
||||
fn from_source_ref(source: &Request) -> Self {
|
||||
Self(source.amount_spent)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_your_thing<F, Extractors, Marker: FromMarker>(f: F)
|
||||
where
|
||||
F: IntoHandler<Extractors, (), Request, Marker>,
|
||||
{
|
||||
let request = Request {
|
||||
customer: "Faren Arbor".into(),
|
||||
order_id: 894127642,
|
||||
amount_spent: 21.48,
|
||||
};
|
||||
|
||||
f.call(request);
|
||||
}
|
||||
}
|
||||
|
||||
mod userspace {
|
||||
use super::library::*;
|
||||
|
||||
fn endpoint_empty() {
|
||||
println!("I'm content not receiving anything!");
|
||||
}
|
||||
|
||||
fn endpoint_wants_to_consume(Customer(customer): Customer) {
|
||||
println!("But I'd appreciate knowing that the customer is {customer}!");
|
||||
}
|
||||
|
||||
fn endpoint_wants_to_share(OrderId(order_id): OrderId, AmountSpent(amount_spent): AmountSpent) {
|
||||
println!("so you said they spent {amount_spent} as part of order {order_id}.. now that's interesting...");
|
||||
}
|
||||
|
||||
fn endpoint_wants_the_original_request(request: Request) {
|
||||
println!("I don't care how invasive it is. I'm glad I can see the whole {request:#?}");
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
println!("Hello world! From userspace!");
|
||||
|
||||
run_your_thing(endpoint_empty);
|
||||
run_your_thing(endpoint_wants_to_consume);
|
||||
run_your_thing(endpoint_wants_to_share);
|
||||
run_your_thing(endpoint_wants_the_original_request);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
userspace::main();
|
||||
}
|
60
src/main.rs
Normal file
60
src/main.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
#[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());
|
||||
}
|
Reference in New Issue
Block a user