From 96013802eb8cb8da86b5f6398e88375203cd5e28 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 23 Jul 2015 22:57:43 +0200 Subject: Support dynamic selection of decoders/encoders --- src/format/binary.rs | 3 ++- src/format/energymech.rs | 3 ++- src/format/mod.rs | 18 ++++++++++++++++-- src/format/weechat3.rs | 5 +++-- 4 files changed, 23 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/format/binary.rs b/src/format/binary.rs index 4c2e151..a1ca12d 100644 --- a/src/format/binary.rs +++ b/src/format/binary.rs @@ -44,7 +44,8 @@ impl<'a, W> Encode<'a, W> for Binary where W: Write { } } -impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Binary where R: BufRead { +impl<'a, R: 'a> Decode<'a, R> for Binary where R: BufRead { + type Output = Iter<'a, R>; fn decode(&'a mut self, _context: &'a Context, input: R) -> Iter { Iter { _phantom: PhantomData, input: input } } diff --git a/src/format/energymech.rs b/src/format/energymech.rs index 1fdc537..0e46dce 100644 --- a/src/format/energymech.rs +++ b/src/format/energymech.rs @@ -151,7 +151,8 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { } } -impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Energymech where R: BufRead { +impl<'a, R: 'a> Decode<'a, R> for Energymech where R: BufRead { + type Output = Iter<'a, R>; fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { Iter { context: context, diff --git a/src/format/mod.rs b/src/format/mod.rs index db86a1d..2a36b34 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -30,8 +30,22 @@ pub trait Encode<'a, W> where W: Write { fn encode(&'a self, context: &'a Context, output: W, event: &'a Event) -> ::Result<()>; } -pub trait Decode<'a, R, O> where R: BufRead, O: Iterator>> { - fn decode(&'a mut self, context: &'a Context, input: R) -> O; +pub trait Decode<'a, Input> where Input: BufRead, + Self::Output: Iterator>> + 'a { + type Output; + fn decode(&'a mut self, context: &'a Context, input: Input) -> Self::Output; +} + +pub trait DecodeBox<'a, I> { + fn decode_box(&'a mut self, context: &'a Context, input: I) + -> Box> + 'a>; +} + +impl<'a, T, I: BufRead> DecodeBox<'a, I> for T where T: Decode<'a, I> { + fn decode_box(&'a mut self, context: &'a Context, input: I) + -> Box> + 'a> { + Box::new(self.decode(context, input)) + } } fn rejoin(s: &[&str], splits: &[char]) -> Cow<'static, str> { diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index 9e07e92..3ff781c 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -137,8 +137,9 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { } } -impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Weechat3 where R: BufRead { - fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { +impl<'a, I: 'a> Decode<'a, I> for Weechat3 where I: BufRead { + type Output = Iter<'a, I>; + fn decode(&'a mut self, context: &'a Context, input: I) -> Iter<'a, I> { Iter { context: context, input: input, -- cgit v1.2.3