Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows deserializing Slab without AccountTag checks #3

Merged
merged 1 commit into from
Aug 14, 2022
Merged
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
19 changes: 19 additions & 0 deletions program/src/state/critbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ impl<'a, C: Pod> Slab<'a, C> {
callback_infos: bytemuck::cast_slice_mut::<_, C>(callback_infos),
})
}

pub fn from_buffer_unchecked(buf: &'a mut [u8]) -> Result<Self, ProgramError> {
let callback_info_len = std::mem::size_of::<C>();
let leaf_size = LeafNode::LEN + callback_info_len;
let capacity = (buf.len() - SlabHeader::LEN - 8 - leaf_size) / (leaf_size + InnerNode::LEN);

let (_, rem) = buf.split_at_mut(8);
let (header, rem) = rem.split_at_mut(SlabHeader::LEN);
let (leaves, rem) = rem.split_at_mut((capacity + 1) * LeafNode::LEN);
let (inner_nodes, callback_infos) = rem.split_at_mut(capacity * InnerNode::LEN);
let header = bytemuck::from_bytes_mut::<SlabHeader>(header);

Ok(Self {
header,
leaf_nodes: bytemuck::cast_slice_mut::<_, LeafNode>(leaves),
inner_nodes: bytemuck::cast_slice_mut::<_, InnerNode>(inner_nodes),
callback_infos: bytemuck::cast_slice_mut::<_, C>(callback_infos),
})
}
}

impl<'a, C> Slab<'a, C> {
Expand Down