diff options
author | Aymeric Fromherz | 2024-05-24 15:02:26 +0200 |
---|---|---|
committer | Aymeric Fromherz | 2024-05-24 15:02:26 +0200 |
commit | 4d33ea68ca1ebfca35b7d7332f63b74dd3c06838 (patch) | |
tree | 838da53ae7e5be27e1dde684d0354a5ce2a1fd99 /tests/src/demo.rs | |
parent | ac5f261997079002a782217ebf0c854e31bb880d (diff) | |
parent | 3c8ea6df20f92be9c341bbfb748f65d6c598fead (diff) |
Merge remote-tracking branch 'origin/main' into afromher_debug
Diffstat (limited to '')
-rw-r--r-- | tests/src/demo.rs | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/src/demo.rs b/tests/src/demo.rs new file mode 100644 index 00000000..bc74cc8b --- /dev/null +++ b/tests/src/demo.rs @@ -0,0 +1,111 @@ +#![allow(clippy::needless_lifetimes)] + +/* Simple functions */ + +pub fn choose<'a, T>(b: bool, x: &'a mut T, y: &'a mut T) -> &'a mut T { + if b { + x + } else { + y + } +} + +pub fn mul2_add1(x: u32) -> u32 { + (x + x) + 1 +} + +pub fn use_mul2_add1(x: u32, y: u32) -> u32 { + mul2_add1(x) + y +} + +pub fn incr<'a>(x: &'a mut u32) { + *x += 1; +} + +pub fn use_incr() { + let mut x = 0; + incr(&mut x); + incr(&mut x); + incr(&mut x); +} + +/* Recursion, loops */ + +pub enum CList<T> { + CCons(T, Box<CList<T>>), + CNil, +} + +pub fn list_nth<'a, T>(l: &'a CList<T>, i: u32) -> &'a T { + match l { + CList::CCons(x, tl) => { + if i == 0 { + x + } else { + list_nth(tl, i - 1) + } + } + CList::CNil => { + panic!() + } + } +} + +pub fn list_nth_mut<'a, T>(l: &'a mut CList<T>, i: u32) -> &'a mut T { + match l { + CList::CCons(x, tl) => { + if i == 0 { + x + } else { + list_nth_mut(tl, i - 1) + } + } + CList::CNil => { + panic!() + } + } +} + +pub fn list_nth_mut1<'a, T>(mut l: &'a mut CList<T>, mut i: u32) -> &'a mut T { + while let CList::CCons(x, tl) = l { + if i == 0 { + return x; + } + i -= 1; + l = tl; + } + panic!() +} + +pub fn i32_id(i: i32) -> i32 { + if i == 0 { + 0 + } else { + i32_id(i - 1) + 1 + } +} + +pub fn list_tail<'a, T>(l: &'a mut CList<T>) -> &'a mut CList<T> { + match l { + CList::CCons(_, tl) => list_tail(tl), + CList::CNil => l, + } +} + +/* Traits */ + +pub trait Counter { + fn incr(&mut self) -> usize; +} + +impl Counter for usize { + fn incr(&mut self) -> usize { + let x = *self; + *self += 1; + x + } +} + +pub fn use_counter<'a, T: Counter>(cnt: &'a mut T) -> usize { + cnt.incr() +} |