Skip to content

Commit

Permalink
remove Ints
Browse files Browse the repository at this point in the history
we didn't really need them anyway
  • Loading branch information
noib3 committed Jan 27, 2024
1 parent d295996 commit b81ba8e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 71 deletions.
14 changes: 7 additions & 7 deletions src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ impl InnerAnchor {
#[cfg(feature = "encode")]
mod encode {
use super::*;
use crate::encode::{Decode, Encode, Int, IntDecodeError};
use crate::encode::{Decode, Encode, IntDecodeError};

impl Encode for InnerAnchor {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.replica_id()).encode(buf);
Int::new(self.run_ts()).encode(buf);
Int::new(self.offset()).encode(buf);
self.replica_id().encode(buf);
self.run_ts().encode(buf);
self.offset().encode(buf);
}
}

Expand All @@ -172,9 +172,9 @@ mod encode {

#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (replica_id, buf) = Int::<ReplicaId>::decode(buf)?;
let (run_ts, buf) = Int::<RunTs>::decode(buf)?;
let (offset, buf) = Int::<Length>::decode(buf)?;
let (replica_id, buf) = ReplicaId::decode(buf)?;
let (run_ts, buf) = RunTs::decode(buf)?;
let (offset, buf) = Length::decode(buf)?;
let anchor = Self::new(replica_id, offset, run_ts);
Ok((anchor, buf))
}
Expand Down
42 changes: 18 additions & 24 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,8 @@ impl core::fmt::Display for BoolDecodeError {
/// - `255` is encoded as `[255]`;
///
/// - numbers greater than 255 are always encoded as `[length, ..bytes..]`.
pub(crate) struct Int<I>(I);

impl<I> Int<I> {
#[inline]
pub(crate) fn new(integer: I) -> Self {
Self(integer)
}
}
#[allow(dead_code)]
struct Int<I>(I);

/// An error that can occur when decoding an [`Int`].
#[cfg_attr(test, derive(PartialEq, Eq))]
Expand Down Expand Up @@ -166,30 +160,30 @@ impl_int_decode!(u16);
impl_int_decode!(u32);
impl_int_decode!(u64);

impl Encode for Int<usize> {
impl Encode for usize {
#[inline(always)]
fn encode(&self, buf: &mut Vec<u8>) {
Int(self.0 as u64).encode(buf)
(*self as u64).encode(buf)
}
}

impl Decode for Int<usize> {
impl Decode for usize {
type Value = usize;

type Error = IntDecodeError;

#[inline(always)]
fn decode(buf: &[u8]) -> Result<(usize, &[u8]), Self::Error> {
Int::<u64>::decode(buf).map(|(value, rest)| (value as usize, rest))
u64::decode(buf).map(|(value, rest)| (value as usize, rest))
}
}

macro_rules! impl_int_encode {
($ty:ty) => {
impl Encode for Int<$ty> {
impl Encode for $ty {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
let int = self.0;
let int = *self;

// We can encode the entire integer with a single byte if it
// falls within this range.
Expand Down Expand Up @@ -221,8 +215,8 @@ use impl_int_encode;

macro_rules! impl_int_decode {
($ty:ty) => {
impl Decode for Int<$ty> {
type Value = $ty;
impl Decode for $ty {
type Value = Self;

type Error = $crate::encode::IntDecodeError;

Expand Down Expand Up @@ -367,9 +361,9 @@ mod tests {
let mut buf = Vec::new();

for int in ints {
Int::new(int).encode(&mut buf);
int.encode(&mut buf);
assert_eq!(buf.len(), 1);
let (decoded, rest) = Int::<u64>::decode(&buf).unwrap();
let (decoded, rest) = u64::decode(&buf).unwrap();
assert_eq!(int, decoded);
assert!(rest.is_empty());
buf.clear();
Expand Down Expand Up @@ -398,7 +392,7 @@ mod tests {
};

for int in ints {
Int::new(int).encode(&mut buf);
int.encode(&mut buf);

let expected_len = (1..=8)
.map(|n_bytes| (n_bytes, max_num_with_n_bytes(n_bytes)))
Expand All @@ -411,7 +405,7 @@ mod tests {

assert_eq!(buf[1..].len() as u8, expected_len);

let (decoded, rest) = Int::<u64>::decode(&buf).unwrap();
let (decoded, rest) = u64::decode(&buf).unwrap();

assert_eq!(int, decoded);

Expand All @@ -426,12 +420,12 @@ mod tests {
fn encode_int_fails_if_buffer_empty() {
let mut buf = Vec::new();

Int::new(42u32).encode(&mut buf);
42u32.encode(&mut buf);

buf.clear();

assert_eq!(
Int::<u32>::decode(&buf).unwrap_err(),
u32::decode(&buf).unwrap_err(),
IntDecodeError::EmptyBuffer
);
}
Expand All @@ -442,12 +436,12 @@ mod tests {
fn encode_int_fails_if_buffer_too_short() {
let mut buf = Vec::new();

Int::new(u8::MAX as u16 + 1).encode(&mut buf);
(u8::MAX as u16 + 1).encode(&mut buf);

buf.pop();

assert_eq!(
Int::<u32>::decode(&buf).unwrap_err(),
u32::decode(&buf).unwrap_err(),
IntDecodeError::LengthLessThanPrefix { prefix: 2, actual: 1 }
);
}
Expand Down
17 changes: 10 additions & 7 deletions src/gtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const _NODE_IDX_LAYOUT_CHECK: usize = {
///
/// TODO: finish describing the data structure.
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "encode", derive(serde::Serialize, serde::Deserialize))]
#[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 @@ -3500,12 +3500,12 @@ mod iter {
#[cfg(feature = "encode")]
mod encode {
use super::*;
use crate::encode::{Decode, Encode, Int, IntDecodeError};
use crate::encode::{Decode, Encode, IntDecodeError};

impl Encode for InodeIdx {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.0).encode(buf);
self.0.encode(buf);
}
}

Expand All @@ -3516,15 +3516,15 @@ mod encode {

#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (idx, rest) = Int::<usize>::decode(buf)?;
let (idx, rest) = usize::decode(buf)?;
Ok((Self(idx), rest))
}
}

impl<L> Encode for LeafIdx<L> {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.idx).encode(buf);
self.idx.encode(buf);
}
}

Expand All @@ -3535,12 +3535,15 @@ mod encode {

#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (idx, rest) = Int::<usize>::decode(buf)?;
let (idx, rest) = usize::decode(buf)?;
Ok((Self::new(idx), rest))
}
}

impl<const N: usize, L: Leaf> Encode for Inode<N, L> {
impl<const N: usize, L: Leaf> Encode for Inode<N, L>
where
L::Length: Encode,
{
#[inline]
fn encode(&self, _buf: &mut Vec<u8>) {
todo!();
Expand Down
16 changes: 5 additions & 11 deletions src/insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ impl Insertion {
#[cfg(feature = "encode")]
mod encode {
use super::*;
use crate::encode::{
BoolDecodeError,
Decode,
Encode,
Int,
IntDecodeError,
};
use crate::encode::{BoolDecodeError, Decode, Encode, IntDecodeError};

impl Insertion {
#[inline]
Expand Down Expand Up @@ -134,8 +128,8 @@ mod encode {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
self.text.encode(buf);
Int::new(self.run_ts).encode(buf);
Int::new(self.lamport_ts).encode(buf);
self.run_ts.encode(buf);
self.lamport_ts.encode(buf);
let run = InsertionRun::new(self);
run.encode(buf);
self.encode_anchor(run, buf);
Expand Down Expand Up @@ -181,8 +175,8 @@ mod encode {
#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (text, buf) = Text::decode(buf)?;
let (run_ts, buf) = Int::<RunTs>::decode(buf)?;
let (lamport_ts, buf) = Int::<LamportTs>::decode(buf)?;
let (run_ts, buf) = RunTs::decode(buf)?;
let (lamport_ts, buf) = LamportTs::decode(buf)?;
let (run, buf) = InsertionRun::decode(buf)?;
let (anchor, buf) = Self::decode_anchor(run, &text, run_ts, buf)?;
let insertion = Self::new(anchor, text, lamport_ts, run_ts);
Expand Down
29 changes: 14 additions & 15 deletions src/run_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,6 @@ mod encode {
Decode,
DecodeWithCtx,
Encode,
Int,
IntDecodeError,
};
use crate::gtree::{Inode, InodeIdx, Lnode};
Expand All @@ -1171,15 +1170,15 @@ mod encode {
fn encode(&self, buf: &mut Vec<u8>) {
let indices = self.run_indices.iter();

Int::new(indices.len() as u64).encode(buf);
(indices.len() as u64).encode(buf);

for (&replica_id, indices) in indices {
ReplicaRuns::new(replica_id, indices, &self.gtree).encode(buf);
}

let inodes = self.gtree.inodes();

Int::new(inodes.len() as u64).encode(buf);
(inodes.len() as u64).encode(buf);

for inode in inodes {
inode.encode(buf);
Expand Down Expand Up @@ -1236,7 +1235,7 @@ mod encode {

#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (num_replicas, mut buf) = Int::<u64>::decode(buf)?;
let (num_replicas, mut buf) = u64::decode(buf)?;

let mut ctx = RunTreeDecodeCtx::default();

Expand All @@ -1247,7 +1246,7 @@ mod encode {

let RunTreeDecodeCtx { lnodes, run_indices, .. } = ctx;

let (num_inodes, mut buf) = Int::<u64>::decode(buf)?;
let (num_inodes, mut buf) = u64::decode(buf)?;

let mut inodes = Vec::new();

Expand Down Expand Up @@ -1287,8 +1286,8 @@ mod encode {
impl Encode for ReplicaRuns<'_> {
#[inline(always)]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.replica_id).encode(buf);
Int::new(self.runs.len() as RunTs).encode(buf);
self.replica_id.encode(buf);
(self.runs.len() as RunTs).encode(buf);

for (fragments, _) in self.runs.iter() {
RunFragments::new(fragments, self.gtree).encode(buf);
Expand All @@ -1308,9 +1307,9 @@ mod encode {
buf: &'buf [u8],
ctx: &mut Self::Ctx,
) -> Result<(Self::Value, &'buf [u8]), Self::Error> {
let (replica_id, buf) = Int::<ReplicaId>::decode(buf)?;
let (replica_id, buf) = ReplicaId::decode(buf)?;

let (num_runs, mut buf) = Int::<RunTs>::decode(buf)?;
let (num_runs, mut buf) = RunTs::decode(buf)?;

ctx.replica_id = replica_id;

Expand Down Expand Up @@ -1345,7 +1344,7 @@ mod encode {
impl Encode for RunFragments<'_> {
#[inline(always)]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.fragments.len() as u64).encode(buf);
(self.fragments.len() as u64).encode(buf);

for fragment in self.fragments.iter() {
RunFragment::new(fragment.leaf_idx(), self.gtree).encode(buf);
Expand All @@ -1365,7 +1364,7 @@ mod encode {
buf: &'buf [u8],
ctx: &mut Self::Ctx,
) -> Result<(Self::Value, &'buf [u8]), Self::Error> {
let (num_fragments, mut buf) = Int::<u64>::decode(buf)?;
let (num_fragments, mut buf) = u64::decode(buf)?;

let initial_temporal_offset = ctx.temporal_offset;

Expand Down Expand Up @@ -1398,8 +1397,8 @@ mod encode {
fn encode(&self, buf: &mut Vec<u8>) {
let edit_run = self.gtree.leaf(self.leaf_idx);

Int::new(edit_run.text.len()).encode(buf);
Int::new(edit_run.lamport_ts).encode(buf);
edit_run.text.len().encode(buf);
edit_run.lamport_ts.encode(buf);
edit_run.is_deleted.encode(buf);
self.leaf_idx.encode(buf);
self.gtree.parent(self.leaf_idx).encode(buf);
Expand All @@ -1418,8 +1417,8 @@ mod encode {
buf: &'buf [u8],
ctx: &mut Self::Ctx,
) -> Result<(Self::Value, &'buf [u8]), Self::Error> {
let (len, buf) = Int::<Length>::decode(buf)?;
let (lamport_ts, buf) = Int::<LamportTs>::decode(buf)?;
let (len, buf) = Length::decode(buf)?;
let (lamport_ts, buf) = LamportTs::decode(buf)?;
let (is_deleted, buf) = bool::decode(buf)?;
let (leaf_idx, buf) = LeafIdx::<EditRun>::decode(buf)?;
let (parent_idx, buf) = InodeIdx::decode(buf)?;
Expand Down
14 changes: 7 additions & 7 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,21 @@ impl Text {
#[cfg(feature = "encode")]
mod encode {
use super::*;
use crate::encode::{Decode, Encode, Int, IntDecodeError};
use crate::encode::{Decode, Encode, IntDecodeError};

impl Encode for Text {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.inserted_by).encode(buf);
Int::new(self.start()).encode(buf);
self.inserted_by.encode(buf);
self.start().encode(buf);
// We encode the length of the text because it's often smaller than
// its end, especially for longer editing sessions.
//
// For example, if a user inserts a character after already having
// inserted 1000 before, it's better to encode `1000, 1` rather
// than `1000, 1001`.
let len = self.end() - self.start();
Int::new(len).encode(buf);
len.encode(buf);
}
}

Expand All @@ -137,9 +137,9 @@ mod encode {

#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (inserted_by, buf) = Int::<ReplicaId>::decode(buf)?;
let (start, buf) = Int::<usize>::decode(buf)?;
let (len, buf) = Int::<usize>::decode(buf)?;
let (inserted_by, buf) = ReplicaId::decode(buf)?;
let (start, buf) = usize::decode(buf)?;
let (len, buf) = usize::decode(buf)?;
let text = Self { inserted_by, range: start..start + len };
Ok((text, buf))
}
Expand Down

0 comments on commit b81ba8e

Please sign in to comment.