forked from nivaldoh/beam
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/statically-typed-pipeline' into rust_sdk
- Loading branch information
Showing
20 changed files
with
383 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use std::{fmt, marker::PhantomData}; | ||
|
||
use integer_encoding::VarInt; | ||
|
||
use crate::{ | ||
coders::{ | ||
required_coders::{BytesCoder, Iterable, IterableCoder, KVCoder, KV}, | ||
standard_coders::{StrUtf8Coder, VarIntCoder}, | ||
CoderI, | ||
}, | ||
elem_types::ElemType, | ||
}; | ||
|
||
/// Resolve a coder (implementing `CoderI) from a coder URN and an `ElemType`. | ||
/// | ||
/// You may use original coders by implementing the `CoderResolver` trait. | ||
pub trait CoderResolver { | ||
type E: ElemType; | ||
type C: CoderI<E = Self::E>; | ||
|
||
/// Resolve a coder from a coder URN. | ||
/// | ||
/// # Returns | ||
/// | ||
/// `Some(C)` if the coder was resolved, `None` otherwise. | ||
fn resolve(coder_urn: &str) -> Option<Self::C> { | ||
(coder_urn == Self::C::get_coder_urn()).then_some(Self::C::default()) | ||
} | ||
} | ||
|
||
/// `Vec<u8>` -> `BytesCoder`. | ||
#[derive(Debug)] | ||
pub struct BytesCoderResolverDefault; | ||
|
||
impl CoderResolver for BytesCoderResolverDefault { | ||
type E = Vec<u8>; | ||
type C = BytesCoder; | ||
} | ||
|
||
/// `KV` -> `KVCoder`. | ||
#[derive(Debug)] | ||
pub struct KVCoderResolverDefault<K, V> { | ||
phantom: PhantomData<KV<K, V>>, | ||
} | ||
|
||
impl<K, V> CoderResolver for KVCoderResolverDefault<K, V> | ||
where | ||
K: Clone + fmt::Debug + Send + Sync + 'static, | ||
V: Clone + fmt::Debug + Send + Sync + 'static, | ||
{ | ||
type E = KV<K, V>; | ||
type C = KVCoder<Self::E>; | ||
} | ||
|
||
/// `Iterable` -> `IterableCoder`. | ||
#[derive(Debug)] | ||
pub struct IterableCoderResolverDefault<E> | ||
where | ||
E: ElemType, | ||
{ | ||
phantom: PhantomData<E>, | ||
} | ||
|
||
impl<E> CoderResolver for IterableCoderResolverDefault<E> | ||
where | ||
E: ElemType + fmt::Debug, | ||
{ | ||
type E = Iterable<E>; | ||
type C = IterableCoder<Self::E>; | ||
} | ||
|
||
/// `String` -> `StrUtf8Coder`. | ||
#[derive(Debug)] | ||
pub struct StrUtf8CoderResolverDefault; | ||
|
||
impl CoderResolver for StrUtf8CoderResolverDefault { | ||
type E = String; | ||
type C = StrUtf8Coder; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct VarIntCoderResolverDefault<N: fmt::Debug + VarInt> { | ||
phantom: PhantomData<N>, | ||
} | ||
|
||
impl CoderResolver for VarIntCoderResolverDefault<i8> { | ||
type E = i8; | ||
type C = VarIntCoder<i8>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<i16> { | ||
type E = i16; | ||
type C = VarIntCoder<i16>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<i32> { | ||
type E = i32; | ||
type C = VarIntCoder<i32>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<i64> { | ||
type E = i64; | ||
type C = VarIntCoder<i64>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<isize> { | ||
type E = isize; | ||
type C = VarIntCoder<isize>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<u8> { | ||
type E = u8; | ||
type C = VarIntCoder<u8>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<u16> { | ||
type E = u16; | ||
type C = VarIntCoder<u16>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<u32> { | ||
type E = u32; | ||
type C = VarIntCoder<u32>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<u64> { | ||
type E = u64; | ||
type C = VarIntCoder<u64>; | ||
} | ||
impl CoderResolver for VarIntCoderResolverDefault<usize> { | ||
type E = usize; | ||
type C = VarIntCoder<usize>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.