From 7003270834f080c221eb9ea30075cf27a0a6fedb Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Sun, 15 Jan 2017 16:34:37 -0500 Subject: [PATCH] Removed parameterization of LineCodec on encoding. --- src/proto/line.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/proto/line.rs b/src/proto/line.rs index 5a1f575..40e4076 100644 --- a/src/proto/line.rs +++ b/src/proto/line.rs @@ -1,16 +1,29 @@ -//! Implementation of line-based codec for Tokio. +//! Implementation of line-delimiting codec for Tokio. use std::io; use std::io::prelude::*; -use encoding::{DecoderTrap, EncoderTrap, Encoding}; +use encoding::{DecoderTrap, EncoderTrap, EncodingRef}; +use encoding::label::encoding_from_whatwg_label; use tokio_core::io::{Codec, EasyBuf}; /// A line-based codec parameterized by an encoding. -pub struct LineCodec { - encoding: E, +pub struct LineCodec { + encoding: EncodingRef, } -impl Codec for LineCodec where E: Encoding { +impl LineCodec { + /// Creates a new instance of LineCodec from the specified encoding. + pub fn new(label: &str) -> io::Result { + encoding_from_whatwg_label(label).map(|enc| LineCodec { encoding: enc }).ok_or( + io::Error::new( + io::ErrorKind::InvalidInput, + &format!("Attempted to use unknown codec {}.", label)[..] + ) + ) + } +} + +impl Codec for LineCodec { type In = String; type Out = String;