Skip to content

Commit

Permalink
Move the MergeFunction trait into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Nov 25, 2024
1 parent 58ac87d commit 32ed23d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
48 changes: 2 additions & 46 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,17 @@ mod block_writer;
mod compression;
mod count_write;
mod error;
mod merge_function;
mod merger;
mod metadata;
mod reader;
mod sorter;
mod varint;
mod writer;

use either::Either;

pub use self::compression::CompressionType;
pub use self::error::Error;
pub use self::merge_function::MergeFunction;
pub use self::merger::{Merger, MergerBuilder, MergerIter};
pub use self::metadata::FileVersion;
pub use self::reader::{PrefixIter, RangeIter, Reader, ReaderCursor, RevPrefixIter, RevRangeIter};
Expand All @@ -219,50 +219,6 @@ pub use self::writer::{Writer, WriterBuilder};

pub type Result<T, U = Infallible> = std::result::Result<T, Error<U>>;

// TODO move this elsewhere
pub trait MergeFunction {
type Error;
fn merge<'a>(
&self,
key: &[u8],
values: &[Cow<'a, [u8]>],
) -> std::result::Result<Cow<'a, [u8]>, Self::Error>;
}

impl<MF> MergeFunction for &MF
where
MF: MergeFunction,
{
type Error = MF::Error;

fn merge<'a>(
&self,
key: &[u8],
values: &[Cow<'a, [u8]>],
) -> std::result::Result<Cow<'a, [u8]>, Self::Error> {
(*self).merge(key, values)
}
}

impl<MFA, MFB> MergeFunction for Either<MFA, MFB>
where
MFA: MergeFunction,
MFB: MergeFunction<Error = MFA::Error>,
{
type Error = MFA::Error;

fn merge<'a>(
&self,
key: &[u8],
values: &[Cow<'a, [u8]>],
) -> std::result::Result<Cow<'a, [u8]>, Self::Error> {
match self {
Either::Left(mfa) => mfa.merge(key, values),
Either::Right(mfb) => mfb.merge(key, values),
}
}
}

/// Sometimes we need to use an unsafe trick to make the compiler happy.
/// You can read more about the issue [on the Rust's Github issues].
///
Expand Down
46 changes: 46 additions & 0 deletions src/merge_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::borrow::Cow;
use std::result::Result;

use either::Either;

/// A trait defining the way we merge multiple
/// values sharing the same key.
pub trait MergeFunction {
type Error;
fn merge<'a>(&self, key: &[u8], values: &[Cow<'a, [u8]>])
-> Result<Cow<'a, [u8]>, Self::Error>;
}

impl<MF> MergeFunction for &MF
where
MF: MergeFunction,
{
type Error = MF::Error;

fn merge<'a>(
&self,
key: &[u8],
values: &[Cow<'a, [u8]>],
) -> Result<Cow<'a, [u8]>, Self::Error> {
(*self).merge(key, values)
}
}

impl<MFA, MFB> MergeFunction for Either<MFA, MFB>
where
MFA: MergeFunction,
MFB: MergeFunction<Error = MFA::Error>,
{
type Error = MFA::Error;

fn merge<'a>(
&self,
key: &[u8],
values: &[Cow<'a, [u8]>],
) -> Result<Cow<'a, [u8]>, Self::Error> {
match self {
Either::Left(mfa) => mfa.merge(key, values),
Either::Right(mfb) => mfb.merge(key, values),
}
}
}

0 comments on commit 32ed23d

Please sign in to comment.