Skip to content

Instantly share code, notes, and snippets.

@mark-i-m
Last active May 19, 2017 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-i-m/7045422098699044ddc87deb7be6fe7f to your computer and use it in GitHub Desktop.
Save mark-i-m/7045422098699044ddc87deb7be6fe7f to your computer and use it in GitHub Desktop.
extern crate bytes;
extern crate serde;
extern crate serde_json;
extern crate tokio_io;
extern crate tokio_proto;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::codec::{Decoder, Encoder, Framed};
use tokio_proto::pipeline::ServerProto;
use bytes::BytesMut;
use errors::*;
use tile::TileState;
#[derive(Debug, Deserialize, Serialize)]
pub enum Msg {
GetState,
State(TileState),
}
#[derive(Default)]
pub struct PaintCodec;
impl Decoder for PaintCodec {
type Item = Msg;
type Error = Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
if let Some(i) = (&String::from_utf8_lossy(src.as_ref())).find("END") {
let msg_str = src.split_to(i);
serde_json::from_slice(msg_str.as_ref())
.map(|v| Some(v))
.chain_err(|| "Unable to deserialize message")
} else {
Ok(None)
}
}
}
impl Encoder for PaintCodec {
type Item = Msg;
type Error = Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<()> {
let msg = serde_json::to_string(&item)
.chain_err(|| "Unable to serialize message")?;
dst.extend(msg.as_bytes());
dst.extend(b"END");
Ok(())
}
}
pub struct PaintProto;
impl<T: AsyncRead + AsyncWrite + 'static> ServerProto<T> for PaintProto {
type Request = Msg;
type Response = Msg;
type Transport = Framed<T, PaintCodec>;
type BindTransport = Result<Self::Transport>;
fn bind_transport(&self, io: T) -> Self::BindTransport {
Ok(io.framed(PaintCodec))
}
}
@mark-i-m
Copy link
Author

$ cargo build
   Compiling cpp_demangle v0.2.1
   Compiling backtrace v0.3.1
   Compiling error-chain v0.10.0
   Compiling paint v0.1.0 (file:///home/mark/Documents/home/paint)
error[E0271]: type mismatch resolving `<tokio_io::codec::Framed<T, proto::PaintCodec> as futures::stream::Stream>::Error == std::io::Error`
  --> /home/mark/Documents/home/paint/src/proto.rs:57:43
   |
57 | impl<T: AsyncRead + AsyncWrite + 'static> ServerProto<T> for PaintProto {
   |                                           ^^^^^^^^^^^^^^ expected struct `errors::Error`, found struct `std::io::Error`
   |
   = note: expected type `errors::Error`
              found type `std::io::Error`
   = note: required by `tokio_proto::pipeline::ServerProto`

error[E0271]: type mismatch resolving `<tokio_io::codec::Framed<T, proto::PaintCodec> as futures::sink::Sink>::SinkError == std::io::Error`
  --> /home/mark/Documents/home/paint/src/proto.rs:57:43
   |
57 | impl<T: AsyncRead + AsyncWrite + 'static> ServerProto<T> for PaintProto {
   |                                           ^^^^^^^^^^^^^^ expected struct `errors::Error`, found struct `std::io::Error`
   |
   = note: expected type `errors::Error`
              found type `std::io::Error`
   = note: required by `tokio_proto::pipeline::ServerProto`

error[E0271]: type mismatch resolving `<std::result::Result<tokio_io::codec::Framed<T, proto::PaintCodec>, errors::Error> as futures::future::IntoFuture>::Error == std::io::Error`
  --> /home/mark/Documents/home/paint/src/proto.rs:57:43
   |
57 | impl<T: AsyncRead + AsyncWrite + 'static> ServerProto<T> for PaintProto {
   |                                           ^^^^^^^^^^^^^^ expected struct `errors::Error`, found struct `std::io::Error`
   |
   = note: expected type `errors::Error`
              found type `std::io::Error`
   = note: required by `tokio_proto::pipeline::ServerProto`

error: aborting due to 3 previous errors

error: Could not compile `paint`.

To learn more, run the command again with --verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment