Skip to content
Open

Enums #376

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/last_will.inherit.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"INHERIT_OR_NOT": {
"value": "Left(0x755201bb62b0a8b8d18fd12fc02951ea3998ba42bfc6664daaf8a0d2298cad43cdc21358c7c82f37654275dc2fea8c858adbe97bac92828b498a5a237004db6f)",
"type": "Either<Signature, Either<Signature, Signature>>"
}
"ACTION": "Action::Inherit(0x755201bb62b0a8b8d18fd12fc02951ea3998ba42bfc6664daaf8a0d2298cad43cdc21358c7c82f37654275dc2fea8c858adbe97bac92828b498a5a237004db6f)"
}
18 changes: 12 additions & 6 deletions examples/last_will.simf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* The inheritor can spend the coins if the owner doesn't move the them for 180
* days. The owner has to repeat the covenant when he moves the coins with his
* hot key. The owner can break out of the covenant with his cold key.
*
* Requires -Z enums to run this example.
*/
fn checksig(pk: Pubkey, sig: Signature) {
let msg: u256 = jet::sig_all_hash();
Expand Down Expand Up @@ -40,12 +42,16 @@ fn refresh_spend(hot_sig: Signature) {
recursive_covenant();
}

enum Action {
Inherit(Signature),
ColdSpend(Signature),
HotSpend(Signature),
}

fn main() {
match witness::INHERIT_OR_NOT {
Left(inheritor_sig: Signature) => inherit_spend(inheritor_sig),
Right(cold_or_hot: Either<Signature, Signature>) => match cold_or_hot {
Left(cold_sig: Signature) => cold_spend(cold_sig),
Right(hot_sig: Signature) => refresh_spend(hot_sig),
},
match witness::ACTION {
Action::Inherit(sig: Signature) => inherit_spend(sig),
Action::ColdSpend(sig: Signature) => cold_spend(sig),
Action::HotSpend(sig: Signature) => refresh_spend(sig),
}
}
21 changes: 17 additions & 4 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,28 @@ impl<A: Clone> BTreeSlice<'_, A> {
}
}

/// Index at which [`BTreeSlice`] splits a slice of length `n` into a left
/// and a right subtree.
///
/// Exposed so that consumers which need to *navigate* the balanced tree.
///
/// ## Panics
///
/// `n` must be at least 2.
pub fn btree_split_index(n: usize) -> usize {
debug_assert!(2 <= n);
let next_pow2 = n.next_power_of_two();
debug_assert!(0 < next_pow2 / 2);
debug_assert!(0 < n - next_pow2 / 2);
n - next_pow2 / 2
}

impl<A: Clone> TreeLike for BTreeSlice<'_, A> {
fn as_node(&self) -> Tree<Self> {
match self.0.len() {
0 | 1 => Tree::Nullary,
n => {
let next_pow2 = n.next_power_of_two();
debug_assert!(0 < next_pow2 / 2);
debug_assert!(0 < n - next_pow2 / 2);
let half = n - next_pow2 / 2;
let half = btree_split_index(n);
let left = BTreeSlice::from_slice(&self.0[..half]);
let right = BTreeSlice::from_slice(&self.0[half..]);
Tree::Binary(left, right)
Expand Down
Loading
Loading