Skip to content

Commit

Permalink
remove unused Serialize and Deserialize impls
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Jan 27, 2024
1 parent b5f3f69 commit c559b2d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 345 deletions.
207 changes: 2 additions & 205 deletions src/gtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ const _NODE_IDX_LAYOUT_CHECK: usize = {
///
/// TODO: finish describing the data structure.
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "encode", derive(serde::Serialize, ::serde::Deserialize))]
pub(crate) struct Gtree<const ARITY: usize, L: Leaf> {
/// The internal nodes of the Gtree.
///
Expand Down Expand Up @@ -130,7 +129,6 @@ pub(crate) struct Gtree<const ARITY: usize, L: Leaf> {
/// It can be passed to [`Gtree::inode()`] and [`Gtree::inode_mut()`] to
/// get access to the inode.
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "encode", derive(serde::Serialize, serde::Deserialize))]
pub(crate) struct InodeIdx(usize);

impl InodeIdx {
Expand Down Expand Up @@ -159,7 +157,6 @@ impl InodeIdx {

/// A stable identifier for a particular leaf in the Gtree.
#[derive(Eq)]
#[cfg_attr(feature = "encode", derive(serde::Serialize, serde::Deserialize))]
pub struct LeafIdx<L> {
idx: usize,
_pd: PhantomData<L>,
Expand Down Expand Up @@ -201,7 +198,6 @@ impl<L> LeafIdx<L> {
/// two leaf nodes in the tree, much like a line cursor identifies a position
/// between two characters in a text editor.
#[derive(PartialEq, Eq)]
#[cfg_attr(feature = "encode", derive(serde::Serialize, serde::Deserialize))]
struct Cursor<L: Leaf> {
/// The index of the leaf node that comes *after* the cursor. There always
/// is one because the cursor is never parked after the last leafof the
Expand Down Expand Up @@ -2733,7 +2729,6 @@ impl<const ARITY: usize, L: Leaf> Inode<ARITY, L> {

/// A leaf node of the Gtree.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "encode", derive(serde::Serialize, serde::Deserialize))]
pub(crate) struct Lnode<Leaf> {
/// The value of this leaf node.
value: Leaf,
Expand Down Expand Up @@ -3618,9 +3613,9 @@ pub(crate) mod encode {
let (num_children, buf) = usize::decode(buf)?;
let (has_leaves, mut buf) = bool::decode(buf)?;
let mut children = [NodeIdx::dangling(); N];
for idx in 0..num_children {
for idx in &mut children[..num_children] {
let (node_idx, new_buf) = NodeIdx::decode(buf)?;
children[idx] = node_idx;
*idx = node_idx;
buf = new_buf;
}
let this =
Expand All @@ -3629,201 +3624,3 @@ pub(crate) mod encode {
}
}
}

#[cfg(feature = "encode")]
mod inode_serde {
use serde::ser::SerializeMap;
use serde::{de, ser};

use super::*;

impl<const N: usize, L: Leaf> ser::Serialize for Inode<N, L>
where
Length: ser::Serialize,
{
fn serialize<S: ser::Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(Some(5))?;

map.serialize_entry("tot_len", &self.tot_len)?;
map.serialize_entry("parent", &self.parent)?;
map.serialize_entry("num_children", &self.num_children)?;
map.serialize_entry("has_leaves", &self.has_leaves)?;

match self.children() {
Either::Internal(inode_idxs) => {
map.serialize_entry("children", inode_idxs)?;
},

Either::Leaf(leaf_idxs) => {
map.serialize_entry("children", leaf_idxs)?;
},
}

map.end()
}
}

impl<'de, const N: usize, L: Leaf> de::Deserialize<'de> for Inode<N, L>
where
Length: de::Deserialize<'de>,
{
fn deserialize<D: de::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
struct InodeVisitor<const N: usize, L: Leaf>(PhantomData<L>);

impl<'de, const N: usize, L> de::Visitor<'de> for InodeVisitor<N, L>
where
L: Leaf,
Length: de::Deserialize<'de>,
{
type Value = Inode<N, L>;

#[inline]
fn expecting(
&self,
formatter: &mut core::fmt::Formatter,
) -> core::fmt::Result {
formatter.write_str("a map representing an Inode")
}

#[inline]
fn visit_map<V: de::MapAccess<'de>>(
self,
mut map: V,
) -> Result<Self::Value, V::Error> {
let mut tot_len = None;
let mut parent = None;
let mut num_children = None;
let mut has_leaves = None;
let mut children_vec = None;

while let Some(key) = map.next_key()? {
match key {
"tot_len" => {
if tot_len.is_some() {
return Err(de::Error::duplicate_field(
"tot_len",
));
}
tot_len = Some(map.next_value()?);
},

"parent" => {
if parent.is_some() {
return Err(de::Error::duplicate_field(
"parent",
));
}
parent = Some(map.next_value()?);
},

"num_children" => {
if num_children.is_some() {
return Err(de::Error::duplicate_field(
"num_children",
));
}
num_children = Some(map.next_value()?);
},

"has_leaves" => {
if has_leaves.is_some() {
return Err(de::Error::duplicate_field(
"has_leaves",
));
}
has_leaves = Some(map.next_value()?);
},

"children" => {
if children_vec.is_some() {
return Err(de::Error::duplicate_field(
"children",
));
}
children_vec =
Some(map.next_value::<Vec<usize>>()?);
},

_ => {
return Err(de::Error::unknown_field(
key,
&[
"tot_len",
"parent",
"num_children",
"has_leaves",
"children",
],
));
},
}
}

let tot_len = tot_len
.ok_or_else(|| de::Error::missing_field("tot_len"))?;

let parent = parent
.ok_or_else(|| de::Error::missing_field("parent"))?;

let num_children = num_children.ok_or_else(|| {
de::Error::missing_field("num_children")
})?;

let has_leaves = has_leaves.ok_or_else(|| {
de::Error::missing_field("has_leaves")
})?;

let children_slice = children_vec
.ok_or_else(|| de::Error::missing_field("children"))?;

if children_slice.len() != num_children {
return Err(de::Error::invalid_length(
children_slice.len(),
&num_children.to_string().as_str(),
));
}

if children_slice.len() > N {
return Err(de::Error::invalid_length(
children_slice.len(),
&format!("no more than {N}").as_str(),
));
}

let mut children = [NodeIdx::dangling(); N];

if has_leaves {
for (i, child_idx) in
children_slice.into_iter().enumerate()
{
children[i] =
NodeIdx::from_leaf(LeafIdx::new(child_idx));
}
} else {
for (i, child_idx) in
children_slice.into_iter().enumerate()
{
children[i] =
NodeIdx::from_internal(InodeIdx(child_idx));
}
}

Ok(Inode {
tot_len,
parent,
num_children,
has_leaves,
children,
})
}
}

deserializer.deserialize_map(InodeVisitor(PhantomData))
}
}
}
Loading

0 comments on commit c559b2d

Please sign in to comment.