Skip to content

Commit

Permalink
Merge pull request #362 from Chia-Network/const-nil
Browse files Browse the repository at this point in the history
Replace NodePtr::null() with NodePtr::NIL constant
  • Loading branch information
arvidn authored Jan 9, 2024
2 parents 4e59b6b + d386075 commit 4a2a4f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
14 changes: 6 additions & 8 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ enum ObjectType {

// The top 6 bits of the NodePtr indicate what type of object it is
impl NodePtr {
pub fn nil() -> Self {
Self::new(ObjectType::Bytes, 0)
pub const NIL: Self = Self::new(ObjectType::Bytes, 0);

const fn new(t: ObjectType, idx: usize) -> Self {
assert!(idx <= NODE_PTR_IDX_MASK as usize);
NodePtr(((t as u32) << NODE_PTR_IDX_BITS) | (idx as u32))
}

// TODO: remove this
pub fn hack(val: usize) -> Self {
Self::new(ObjectType::Bytes, val)
}

fn new(t: ObjectType, idx: usize) -> Self {
assert!(idx <= NODE_PTR_IDX_MASK as usize);
NodePtr(((t as u32) << NODE_PTR_IDX_BITS) | (idx as u32))
}

fn node_type(&self) -> (ObjectType, usize) {
(
match self.0 >> NODE_PTR_IDX_BITS {
Expand All @@ -56,7 +54,7 @@ impl NodePtr {

impl Default for NodePtr {
fn default() -> Self {
Self::nil()
Self::NIL
}
}

Expand Down
20 changes: 6 additions & 14 deletions src/op_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn get_args<const N: usize>(
) -> Result<[NodePtr; N], EvalErr> {
let mut next = args;
let mut counter = 0;
let mut ret: [NodePtr; N] = [NodePtr::nil(); N];
let mut ret = [NodePtr::NIL; N];

while let Some((first, rest)) = a.next(next) {
next = rest;
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn get_varargs<const N: usize>(
) -> Result<([NodePtr; N], usize), EvalErr> {
let mut next = args;
let mut counter = 0;
let mut ret: [NodePtr; N] = [NodePtr::nil(); N];
let mut ret = [NodePtr::NIL; N];

while let Some((first, rest)) = a.next(next) {
next = rest;
Expand Down Expand Up @@ -131,27 +131,19 @@ fn test_get_varargs() {
);
assert_eq!(
get_varargs::<4>(&a, args3, "test").unwrap(),
([a1, a2, a3, NodePtr::nil()], 3)
([a1, a2, a3, NodePtr::NIL], 3)
);
assert_eq!(
get_varargs::<4>(&a, args2, "test").unwrap(),
([a2, a3, NodePtr::nil(), NodePtr::nil()], 2)
([a2, a3, NodePtr::NIL, NodePtr::NIL], 2)
);
assert_eq!(
get_varargs::<4>(&a, args1, "test").unwrap(),
([a3, NodePtr::nil(), NodePtr::nil(), NodePtr::nil()], 1)
([a3, NodePtr::NIL, NodePtr::NIL, NodePtr::NIL], 1)
);
assert_eq!(
get_varargs::<4>(&a, args0, "test").unwrap(),
(
[
NodePtr::nil(),
NodePtr::nil(),
NodePtr::nil(),
NodePtr::nil()
],
0
)
([NodePtr::NIL; 4], 0)
);

let r = get_varargs::<3>(&a, args4, "test").unwrap_err();
Expand Down
8 changes: 4 additions & 4 deletions src/test_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn test_pre_eval_and_post_eval() {
&mut allocator,
&ChiaDialect::new(NO_UNKNOWN_OPS),
program,
NodePtr::nil(),
NodePtr::NIL,
COST_LIMIT,
Some(pre_eval_f),
)
Expand All @@ -468,13 +468,13 @@ fn test_pre_eval_and_post_eval() {
let args_consed = allocator.new_pair(a99, a101).unwrap();

let mut desired_outcomes = Vec::new(); // Not in order.
desired_outcomes.push((args, NodePtr::nil(), arg_mid));
desired_outcomes.push((f_quoted, NodePtr::nil(), f_expr));
desired_outcomes.push((args, NodePtr::NIL, arg_mid));
desired_outcomes.push((f_quoted, NodePtr::NIL, f_expr));
desired_outcomes.push((a2, arg_mid, a99));
desired_outcomes.push((a5, arg_mid, a101));
desired_outcomes.push((cons_expr, arg_mid, args_consed));
desired_outcomes.push((f_expr, arg_mid, a99));
desired_outcomes.push((program, NodePtr::nil(), a99));
desired_outcomes.push((program, NodePtr::NIL, a99));

let mut found_outcomes = HashSet::new();
let tracking_examine = tracking.borrow();
Expand Down

0 comments on commit 4a2a4f9

Please sign in to comment.