Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to tokio 0.2(alpha) #21

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ default = ["libudev"]
libudev = ["mio-serial/libudev"]

[dependencies]
futures = "0.1"
tokio-io = "0.1"
tokio-reactor = "0.1"
tokio-io = "=0.2.0-alpha.1"
tokio-reactor = "=0.2.0-alpha.1"
bytes = "0.4"
mio = "0.6"
mio-serial = { version = "=3.2.14", default-features = false }

[dev-dependencies]
tokio = "0.1"
tokio = "=0.2.0-alpha.1"
25 changes: 13 additions & 12 deletions examples/serial_println.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
extern crate bytes;
extern crate futures;
extern crate tokio;
extern crate tokio_io;
extern crate tokio_serial;

use std::{env, io, str};
use tokio_io::codec::{Decoder, Encoder};

use bytes::BytesMut;
use std::{env, io, str};
use tokio::codec::{Decoder, Encoder};

use futures::{Future, Stream};
use tokio::prelude::*;

#[cfg(unix)]
const DEFAULT_TTY: &str = "/dev/ttyUSB0";
Expand Down Expand Up @@ -54,13 +52,16 @@ fn main() {
port.set_exclusive(false)
.expect("Unable to set serial port exlusive");

let (_, reader) = LineCodec.framed(port).split();
let framed = LineCodec.framed(port);

let printer = reader
.for_each(|s| {
println!("{:?}", s);
Ok(())
}).map_err(|e| eprintln!("{}", e));
let printer = framed.for_each(|s| {
match s {
Ok(data) => println!("{:x?}", data),
Err(err) => println!("err: {:x?}", err),
}
tokio::future::ready(())
});

tokio::run(printer);
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(printer);
}
57 changes: 44 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@
#![deny(missing_docs)]

extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_reactor;

extern crate mio_serial;

// Re-export serialport types and traits from mio_serial
pub use mio_serial::{
DataBits, Error, ErrorKind, FlowControl, Parity, SerialPort, SerialPortSettings, StopBits, ClearBuffer,
ClearBuffer, DataBits, Error, ErrorKind, FlowControl, Parity, SerialPort, SerialPortSettings,
StopBits,
};

/// A type for results generated by interacting with serial ports.
pub type Result<T> = mio_serial::Result<T>;

use futures::{Async, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_reactor::{Handle, PollEvented};

use bytes::Buf;
use std::io::{self, Read, Write};
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

/// Serial port I/O struct.
Expand Down Expand Up @@ -227,24 +229,24 @@ impl ::SerialPort for Serial {
self.io.get_ref().clear(buffer_to_clear)
}

fn try_clone(&self) -> ::Result<Box<::SerialPort>> {
fn try_clone(&self) -> ::Result<Box<dyn (::SerialPort)>> {
self.io.get_ref().try_clone()
}
}

impl Read for Serial {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.io.read(buf)
self.io.get_ref().read(buf)
}
}

impl Write for Serial {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.io.write(buf)
self.io.get_ref().write(buf)
}

fn flush(&mut self) -> io::Result<()> {
self.io.flush()
self.io.get_ref().flush()
}
}

Expand All @@ -258,17 +260,46 @@ impl AsRawFd for Serial {
}

impl AsyncRead for Serial {
fn poll_read(&mut self, buf: &mut [u8]) -> io::Result<Async<usize>> {
self.io.poll_read(buf)
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().io).poll_read(cx, buf)
}
}

impl AsyncWrite for Serial {
fn poll_write(&mut self, buf: &[u8]) -> io::Result<Async<usize>> {
self.io.poll_write(buf)
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().io).poll_write(cx, buf)
}

fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), io::Error>> {
Pin::new(&mut self.get_mut().io).poll_shutdown(cx)
}

fn poll_write_buf<B: Buf>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<usize>>
where
Self: Sized,
{
Pin::new(&mut self.get_mut().io).poll_write_buf(cx, buf)
}

fn shutdown(&mut self) -> Poll<(), io::Error> {
self.io.shutdown()
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), io::Error>> {
Pin::new(&mut self.get_mut().io).poll_flush(cx)
}
}