commit bd6251495e359ab37467e60fa80365ab56510ef3 Author: Jacob Babich Date: Wed Dec 27 14:47:49 2023 -0500 meta: initial commit (archival) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1783eec --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +.history/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ec11a26 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c1ccddb --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/src/bin/magical-handler-functions.rs b/src/bin/magical-handler-functions.rs new file mode 100644 index 0000000..7369d0b --- /dev/null +++ b/src/bin/magical-handler-functions.rs @@ -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 { + fn from_source_ref(source: &Source) -> Self; + } + pub trait FromSource { + fn from_source(source: Source) -> Self; + } + + pub trait IntoHandler + where + M: FromMarker, + { + fn call(self, source: Source) -> Return; + } + + // impl IntoHandler<(), Return, Source, Marker> for F + // where + // F: Fn() -> Return, + // Marker: FromMarker, + // { + // fn call(self, _source: Source) -> Return { + // (self)() + // } + // } + + impl IntoHandler<(), Return, Source, FromSourceRefMarker> for F + where + F: Fn() -> Return, + { + fn call(self, _source: Source) -> Return { + (self)() + } + } + + impl IntoHandler<(T1,), Return, Source, FromSourceRefMarker> for F + where + F: Fn(T1) -> Return, + T1: FromSourceRef, + { + fn call(self, source: Source) -> Return { + (self)(T1::from_source_ref(&source)) + } + } + + impl IntoHandler<(T1,), Return, Source, FromSourceMarker> for F + where + F: Fn(T1) -> Return, + T1: FromSource, + { + fn call(self, source: Source) -> Return { + (self)(T1::from_source(source)) + } + } + + impl IntoHandler<(T1, T2), Return, Source, FromSourceRefMarker> for F + where + F: Fn(T1, T2) -> Return, + T1: FromSourceRef, + T2: FromSourceRef, + { + fn call(self, source: Source) -> Return { + (self)(T1::from_source_ref(&source), T2::from_source_ref(&source)) + } + } + + impl IntoHandler<(T1, T2), Return, Source, FromSourceMarker> for F + where + F: Fn(T1, T2) -> Return, + T1: FromSourceRef, + T2: FromSource, + { + fn call(self, source: Source) -> Return { + (self)(T1::from_source_ref(&source), T2::from_source(source)) + } + } + + impl + IntoHandler<(T1, T2, T3), Return, Source, FromSourceRefMarker> for F + where + F: Fn(T1, T2, T3) -> Return, + T1: FromSourceRef, + T2: FromSourceRef, + T3: FromSourceRef, + { + fn call(self, source: Source) -> Return { + (self)( + T1::from_source_ref(&source), + T2::from_source_ref(&source), + T3::from_source_ref(&source), + ) + } + } + + impl IntoHandler<(T1, T2, T3), Return, Source, FromSourceMarker> + for F + where + F: Fn(T1, T2, T3) -> Return, + T1: FromSourceRef, + T2: FromSourceRef, + T3: FromSource, + { + 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 for Request { + fn from_source(source: Request) -> Self { + source + } + } + + pub struct Customer(pub String); + impl FromSource for Customer { + fn from_source(source: Request) -> Self { + Self(source.customer) + } + } + + pub struct OrderId(pub u64); + impl FromSourceRef for OrderId { + fn from_source_ref(source: &Request) -> Self { + Self(source.order_id) + } + } + + pub struct AmountSpent(pub f64); + impl FromSourceRef for AmountSpent { + fn from_source_ref(source: &Request) -> Self { + Self(source.amount_spent) + } + } + + pub fn run_your_thing(f: F) + where + F: IntoHandler, + { + 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(); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4a6a3c6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,60 @@ +#[derive(Clone, Debug)] +struct Context { + id: i32, + param: String, +} + +trait FromSourceRef { + fn from_source_ref(source: &Source) -> Self; +} + +struct Id(pub i32); +impl FromSourceRef for Id { + fn from_source_ref(context: &Context) -> Self { + Id(context.id) + } +} + +struct Param(pub String); +impl FromSourceRef 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 { + fn call(self, context: C); +} + +impl, C> MagicFunction<(T,), C> for F { + fn call(self, context: C) { + (self)(T::from_source_ref(&context)) + } +} +impl, T2: FromSourceRef, 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(function: impl MagicFunction, 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()); +}