Skip to content

Instantly share code, notes, and snippets.

@jjl
Last active June 13, 2023 08:11
Show Gist options
  • Save jjl/e68969856628956866207ac52b5b43bb to your computer and use it in GitHub Desktop.
Save jjl/e68969856628956866207ac52b5b43bb to your computer and use it in GitHub Desktop.
def node_lookup(nodes):
tpl = "pub(crate) unsafe fn {}node_lookup<K: Key>(ptr: {}, key: &K) -> Result<Lookup, LookupError> {{"
"\n\n".join(["".join(tidy(group(
inline(opener),
"let raw = ptr.as_ptr_unchecked() as *mut u8;",
"prefetch_node(raw);",
"let tag = ptr.tag();",
debug("if tag != 0 { debug_assert!((&*raw.cast::<NodeHeader>()).tag == tag); }"),
group(
"match tag {",
"node_tag::NULL => Err(LookupError::Null),",
"node_tag::ELEMENT => Ok(Lookup::Element(ptr)),",
"node_tag::COMPOUND => Ok(Lookup::Compound(ptr)),",
*just_cols(
["node_tag::{}".format(node.tag_const()) for node in nodes],
["=> Ok(Lookup::Index((&*(raw as *const {})).lookup(key))),".format(node.mono()) for node in nodes],
),
"_ => Err(LookupError::CorruptNodeTag(tag)),"
)))) for opener in [
not_storage(tpl.format("", "NodePtr")),
storage( tpl.format("", "MemPtr")),
storage( tpl.format("disk_", "LoadedPtr")),
]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment