-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dynamic capsule support (#10)
Fixes #9
- Loading branch information
1 parent
ca3e17a
commit 1d336b5
Showing
5 changed files
with
329 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "example-fibonacci" | ||
version = "0.4.0" | ||
publish = false | ||
edition.workspace = true | ||
license.workspace = true | ||
description.workspace = true | ||
homepage.workspace = true | ||
documentation.workspace = true | ||
repository.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
authors.workspace = true | ||
readme.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
rearch = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use rearch::{Capsule, CapsuleHandle, CapsuleKey, Container}; | ||
|
||
struct FibonacciCapsule(u8); | ||
impl Capsule for FibonacciCapsule { | ||
type Data = u128; | ||
|
||
fn build(&self, CapsuleHandle { mut get, .. }: CapsuleHandle) -> Self::Data { | ||
let Self(n) = self; | ||
match n { | ||
0 => 0, | ||
1 => 1, | ||
n => get.get(Self(n - 1)) + get.get(Self(n - 2)), | ||
} | ||
} | ||
|
||
fn eq(old: &Self::Data, new: &Self::Data) -> bool { | ||
old == new | ||
} | ||
|
||
fn key(&self) -> CapsuleKey { | ||
let Self(id) = self; | ||
id.to_le_bytes().as_ref().to_owned().into() | ||
} | ||
} | ||
|
||
fn main() { | ||
let container = Container::new(); | ||
println!( | ||
"The 100th fibonacci number is {}", | ||
container.read(FibonacciCapsule(100)), | ||
); | ||
} | ||
|
||
#[test] | ||
fn fib_number_is_correct() { | ||
let container = Container::new(); | ||
assert_eq!( | ||
container.read(FibonacciCapsule(100)), | ||
354_224_848_179_261_915_075 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.