diff --git a/src/allocator.rs b/src/allocator.rs index 1d9f08d1..59ac7d41 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -19,8 +19,11 @@ 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 @@ -28,11 +31,6 @@ impl NodePtr { 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 { @@ -56,7 +54,7 @@ impl NodePtr { impl Default for NodePtr { fn default() -> Self { - Self::nil() + Self::NIL } } diff --git a/src/op_utils.rs b/src/op_utils.rs index 0128a542..b04a6d65 100644 --- a/src/op_utils.rs +++ b/src/op_utils.rs @@ -18,7 +18,7 @@ pub fn get_args( ) -> 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; @@ -91,7 +91,7 @@ pub fn get_varargs( ) -> 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; @@ -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(); diff --git a/src/test_ops.rs b/src/test_ops.rs index 10aa354d..591c20be 100644 --- a/src/test_ops.rs +++ b/src/test_ops.rs @@ -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), ) @@ -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();