Skip to content

Commit

Permalink
Replace NodePtr::null() with NodePtr::NIL constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jan 9, 2024
1 parent 293e9ab commit cdd50a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 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 null() -> 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::null()
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::null(); 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::null(); N];
let mut ret: [NodePtr; N] = [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::null()], 3)
([a1, a2, a3, NodePtr::NIL], 3)
);
assert_eq!(
get_varargs::<4>(&a, args2, "test").unwrap(),
([a2, a3, NodePtr::null(), NodePtr::null()], 2)
([a2, a3, NodePtr::NIL, NodePtr::NIL], 2)
);
assert_eq!(
get_varargs::<4>(&a, args1, "test").unwrap(),
([a3, NodePtr::null(), NodePtr::null(), NodePtr::null()], 1)
([a3, NodePtr::NIL, NodePtr::NIL, NodePtr::NIL], 1)
);
assert_eq!(
get_varargs::<4>(&a, args0, "test").unwrap(),
(
[
NodePtr::null(),
NodePtr::null(),
NodePtr::null(),
NodePtr::null()
],
0
)
([NodePtr::NIL; 4], 0)
);

let r = get_varargs::<3>(&a, args4, "test").unwrap_err();
Expand Down

0 comments on commit cdd50a0

Please sign in to comment.