meta: initial commit (archival)
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
.history/
|
100
Cargo.lock
generated
Normal file
100
Cargo.lock
generated
Normal file
@@ -0,0 +1,100 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hex-literal"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.147"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||
|
||||
[[package]]
|
||||
name = "rust-magic-function-params"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hex-literal",
|
||||
"sha2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.10.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "rust-magic-function-params"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
hex-literal = "0.4.1"
|
||||
sha2 = "0.10.7"
|
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