From dc19902d489a631e310d73c5f16bed0154908cca Mon Sep 17 00:00:00 2001 From: Jacob Babich Date: Wed, 27 Dec 2023 15:12:31 -0500 Subject: [PATCH] initial laptop commit --- .gitignore | 2 ++ Cargo.lock | 7 ++++ Cargo.toml | 8 +++++ examples/access-macro.rs | 63 ++++++++++++++++++++++++++++++++ examples/relate-macro.rs | 78 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 30 ++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 examples/access-macro.rs create mode 100644 examples/relate-macro.rs create mode 100644 src/main.rs 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..f4689ff --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "test-const-traits" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f844506 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "test-const-traits" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/examples/access-macro.rs b/examples/access-macro.rs new file mode 100644 index 0000000..983dca2 --- /dev/null +++ b/examples/access-macro.rs @@ -0,0 +1,63 @@ +macro_rules! access { + ($instance: expr, $trait: path, $with: expr) => { + { + fn get_thing(instance: &T) { + $with(::Type) + } + + get_thing($instance) + } + } +} + +struct Cat; +struct Dog; + +trait Animal { + type Type; +} + +impl Animal for Cat { + type Type = u32; +} + +impl Animal for Dog { + type Type = i64; +} + +trait MaxAndMin { + type Type; + const MAXIMUM: Self::Type; + const MINIMUM: Self::Type; +} + +impl MaxAndMin for u32 { + type Type = u32; + const MAXIMUM: Self::Type = u32::MAX; + const MINIMUM: Self::Type = u32::MIN; +} + +impl MaxAndMin for i64 { + type Type = i64; + const MAXIMUM: Self::Type = i64::MAX; + const MINIMUM: Self::Type = i64::MIN; +} + +fn main() { + let katniss = Cat; + let lola = Dog; + + fn get_min(instance: &T) -> <::Type as MaxAndMin>::Type where T::Type: MaxAndMin { + ::MINIMUM + } + + let katniss_max = get_min(&katniss); + let lola_max = get_min(&lola); + + println!("{katniss_max}"); + println!("{lola_max}"); + + // let max = access!(&katniss, Animal, ::MAX); + + // println!("{legs}"); +} diff --git a/examples/relate-macro.rs b/examples/relate-macro.rs new file mode 100644 index 0000000..ffa2eb0 --- /dev/null +++ b/examples/relate-macro.rs @@ -0,0 +1,78 @@ +struct Katniss; +struct Chiko; +struct Lola; + +struct Cat; +struct Dog; + +struct Jacob; + +macro_rules! relate { + ($in: ty, $out: ty, $forward: ty, $backward: ty) => { + todo!(); + }; +} + +relate!(Jacob, Katniss, Loves, IsLovedBy); + + +// relate_many!(Cat, (Katniss, Chiko), HasMembers, Species); +// relate_many!(Dog, (Lola,), HasMembers, Species); + + +// relate!(); + +// relate_bidirectional!(); + +// relate_many_bidirectional!(); + +// relate_many!(Lola, (Katniss, Chiko), ); + + +fn main() { + +} + + +#[rustfmt::skip] +macro_rules! all_the_tuples { + ($name:ident) => { + $name!([], T1); + $name!([T1], T2); + $name!([T1, T2], T3); + $name!([T1, T2, T3], T4); + $name!([T1, T2, T3, T4], T5); + $name!([T1, T2, T3, T4, T5], T6); + $name!([T1, T2, T3, T4, T5, T6], T7); + $name!([T1, T2, T3, T4, T5, T6, T7], T8); + $name!([T1, T2, T3, T4, T5, T6, T7, T8], T9); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9], T10); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], T11); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11], T12); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12], T13); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13], T14); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14], T15); + $name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15], T16); + }; +} + +macro_rules! all_the_tuples_no_last_special_case { + ($name:ident) => { + $name!(T1); + $name!(T1, T2); + $name!(T1, T2, T3); + $name!(T1, T2, T3, T4); + $name!(T1, T2, T3, T4, T5); + $name!(T1, T2, T3, T4, T5, T6); + $name!(T1, T2, T3, T4, T5, T6, T7); + $name!(T1, T2, T3, T4, T5, T6, T7, T8); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15); + $name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16); + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ca398be --- /dev/null +++ b/src/main.rs @@ -0,0 +1,30 @@ +use core::fmt::Debug; + +trait Value { + type Type; + const VALUE: Self::Type; +} + +fn create_value() -> impl Value { + struct Struct; + impl Value for Struct { + type Type = u32; + const VALUE: Self::Type = 17; + } + + let instance = Struct; + instance +} + +fn print_value(t: T) +where + ::Type: Debug, +{ + let value = T::VALUE; + println!("{value:?}"); +} + +fn main() { + let my_thing = create_value(); + print_value(my_thing); +}