From 32ed23de6e61e01cde040c25f2689715a784fb8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 25 Nov 2024 14:44:28 +0100 Subject: [PATCH] Move the MergeFunction trait into its own module --- src/lib.rs | 48 ++----------------------------------------- src/merge_function.rs | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 src/merge_function.rs diff --git a/src/lib.rs b/src/lib.rs index d96389a..5336df3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -196,6 +196,7 @@ mod block_writer; mod compression; mod count_write; mod error; +mod merge_function; mod merger; mod metadata; mod reader; @@ -203,10 +204,9 @@ 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}; @@ -219,50 +219,6 @@ pub use self::writer::{Writer, WriterBuilder}; pub type Result = std::result::Result>; -// TODO move this elsewhere -pub trait MergeFunction { - type Error; - fn merge<'a>( - &self, - key: &[u8], - values: &[Cow<'a, [u8]>], - ) -> std::result::Result, Self::Error>; -} - -impl MergeFunction for &MF -where - MF: MergeFunction, -{ - type Error = MF::Error; - - fn merge<'a>( - &self, - key: &[u8], - values: &[Cow<'a, [u8]>], - ) -> std::result::Result, Self::Error> { - (*self).merge(key, values) - } -} - -impl MergeFunction for Either -where - MFA: MergeFunction, - MFB: MergeFunction, -{ - type Error = MFA::Error; - - fn merge<'a>( - &self, - key: &[u8], - values: &[Cow<'a, [u8]>], - ) -> std::result::Result, 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]. /// diff --git a/src/merge_function.rs b/src/merge_function.rs new file mode 100644 index 0000000..186a5c8 --- /dev/null +++ b/src/merge_function.rs @@ -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, Self::Error>; +} + +impl MergeFunction for &MF +where + MF: MergeFunction, +{ + type Error = MF::Error; + + fn merge<'a>( + &self, + key: &[u8], + values: &[Cow<'a, [u8]>], + ) -> Result, Self::Error> { + (*self).merge(key, values) + } +} + +impl MergeFunction for Either +where + MFA: MergeFunction, + MFB: MergeFunction, +{ + type Error = MFA::Error; + + fn merge<'a>( + &self, + key: &[u8], + values: &[Cow<'a, [u8]>], + ) -> Result, Self::Error> { + match self { + Either::Left(mfa) => mfa.merge(key, values), + Either::Right(mfb) => mfb.merge(key, values), + } + } +}