initial laptop commit

This commit is contained in:
2023-12-27 15:12:31 -05:00
commit dc19902d48
6 changed files with 188 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
.history/

7
Cargo.lock generated Normal file
View File

@@ -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"

8
Cargo.toml Normal file
View File

@@ -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]

63
examples/access-macro.rs Normal file
View File

@@ -0,0 +1,63 @@
macro_rules! access {
($instance: expr, $trait: path, $with: expr) => {
{
fn get_thing<T: $trait>(instance: &T) {
$with(<T as $trait>::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<T: Animal>(instance: &T) -> <<T as Animal>::Type as MaxAndMin>::Type where T::Type: MaxAndMin {
<T::Type as 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}");
}

78
examples/relate-macro.rs Normal file
View File

@@ -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);
};
}

30
src/main.rs Normal file
View File

@@ -0,0 +1,30 @@
use core::fmt::Debug;
trait Value {
type Type;
const VALUE: Self::Type;
}
fn create_value() -> impl Value<Type = u32> {
struct Struct;
impl Value for Struct {
type Type = u32;
const VALUE: Self::Type = 17;
}
let instance = Struct;
instance
}
fn print_value<T: Value>(t: T)
where
<T as Value>::Type: Debug,
{
let value = T::VALUE;
println!("{value:?}");
}
fn main() {
let my_thing = create_value();
print_value(my_thing);
}