Skip to content

Commit

Permalink
Eio5 (#8)
Browse files Browse the repository at this point in the history
* Update to eio 0.5

* Fix CI

* Fix CI

* Compat with latest embedded-svc
  • Loading branch information
ivmarkov authored Oct 1, 2023
1 parent a7283a3 commit e9f5e2f
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 216 deletions.
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ license = "MIT OR Apache-2.0"
readme = "README.md"
rust-version = "1.67"

[patch.crates-io]
embedded-svc = { git = "https://github.com/esp-rs/embedded-svc" }

[features]
default = ["std"]

std = ["alloc", "embedded-io/std", "embassy-sync/std", "embedded-svc?/std", "async-io", "futures-lite", "httparse/std", "domain?/std"]
alloc = ["embedded-io/alloc", "embedded-svc?/alloc"]
nightly = ["embedded-io/async", "embassy-sync/nightly", "embedded-svc?/nightly"]
std = ["alloc", "embedded-io/std", "embassy-sync/std", "embedded-svc?/std", "async-io", "futures-lite/std", "httparse/std", "domain?/std"]
alloc = ["embedded-io/alloc", "embedded-svc?/alloc", "embedded-io-async?/alloc", ]
nightly = ["embedded-io-async", "embassy-sync/nightly", "embedded-svc?/nightly"]
embassy-util = [] # Just for backward compat. To be removed in future

[dependencies]
embedded-io = { version = "0.4", default-features = false }
embedded-io = { version = "0.5", default-features = false }
embedded-io-async = { version = "0.5", default-features = false, optional = true }
heapless = { version = "0.7", default-features = false }
httparse = { version = "1.7", default-features = false }
embassy-futures = "0.1"
Expand All @@ -30,7 +34,7 @@ no-std-net = { version = "0.6", default-features = false }
log = { version = "0.4", default-features = false }
base64 = { version = "0.13", default-features = false }
sha1_smol = { version = "1", default-features = false }
embedded-nal-async = "0.4"
embedded-nal-async = "0.5"
domain = { version = "0.7", default-features = false, optional = true }
futures-lite = { version = "1", default-features = false, optional = true }
async-io = { version = "1", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;

use embedded_io::asynch::Read;
use embedded_io_async::Read;
use embedded_nal_async::TcpConnect;

use edge_net::asynch::http::client::ClientConnection;
Expand Down
7 changes: 2 additions & 5 deletions examples/http_server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![feature(cfg_version)]
#![cfg_attr(not(version("1.65")), feature(generic_associated_types))]
#![feature(type_alias_impl_trait)]
#![cfg_attr(version("1.67"), allow(incomplete_features))]
#![cfg_attr(version("1.67"), feature(async_fn_in_trait))]
#![feature(async_fn_in_trait)]

use core::future::pending;

Expand All @@ -18,7 +15,7 @@ use edge_net::asynch::{
};
use edge_net::std_mutex::StdRawMutex;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embedded_io::asynch::{Read, Write};
use embedded_io_async::{Read, Write};

fn main() {
simple_logger::SimpleLogger::new()
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_io::asynch::{Read, Write};
use embedded_io_async::{Read, Write};
use embedded_nal_async::TcpConnect;

use edge_net::asynch::stdnal::StdTcpConnect;
Expand Down
133 changes: 86 additions & 47 deletions src/asynch/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use core::cmp::min;
use core::fmt::{Display, Write as _};
use core::str;

use embedded_io::asynch::{Read, Write};
use embedded_io::Io;
use embedded_io::ErrorType;
use embedded_io_async::{Read, Write};

use httparse::{Header, Status, EMPTY_HEADER};

Expand All @@ -12,6 +12,7 @@ use log::trace;
#[cfg(feature = "embedded-svc")]
pub use embedded_svc_compat::*;

use super::io::map_write_err;
use super::ws::http::UpgradeError;

pub mod client;
Expand Down Expand Up @@ -294,10 +295,26 @@ where
body = BodyType::from_header(name, unsafe { str::from_utf8_unchecked(value) });
}

output.write_all(name.as_bytes()).await.map_err(Error::Io)?;
output.write_all(b": ").await.map_err(Error::Io)?;
output.write_all(value).await.map_err(Error::Io)?;
output.write_all(b"\r\n").await.map_err(Error::Io)?;
output
.write_all(name.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output
.write_all(b": ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output
.write_all(value)
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
}

Ok(body)
Expand All @@ -307,7 +324,11 @@ pub async fn send_headers_end<W>(mut output: W) -> Result<(), Error<W::Error>>
where
W: Write,
{
output.write_all(b"\r\n").await.map_err(Error::Io)
output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)
}

#[derive(Debug)]
Expand Down Expand Up @@ -626,9 +647,9 @@ where
}
}

impl<'b, R> Io for Body<'b, R>
impl<'b, R> ErrorType for Body<'b, R>
where
R: Io,
R: ErrorType,
{
type Error = Error<R::Error>;
}
Expand Down Expand Up @@ -674,9 +695,9 @@ impl<'b, R> PartiallyRead<'b, R> {
}
}

impl<'b, R> Io for PartiallyRead<'b, R>
impl<'b, R> ErrorType for PartiallyRead<'b, R>
where
R: Io,
R: ErrorType,
{
type Error = R::Error;
}
Expand Down Expand Up @@ -723,9 +744,9 @@ impl<R> ContentLenRead<R> {
}
}

impl<R> Io for ContentLenRead<R>
impl<R> ErrorType for ContentLenRead<R>
where
R: Io,
R: ErrorType,
{
type Error = Error<R::Error>;
}
Expand Down Expand Up @@ -953,9 +974,9 @@ where
}
}

impl<'b, R> Io for ChunkedRead<'b, R>
impl<'b, R> ErrorType for ChunkedRead<'b, R>
where
R: Io,
R: ErrorType,
{
type Error = Error<R::Error>;
}
Expand Down Expand Up @@ -1037,9 +1058,9 @@ where
}
}

impl<W> Io for SendBody<W>
impl<W> ErrorType for SendBody<W>
where
W: Io,
W: ErrorType,
{
type Error = Error<W::Error>;
}
Expand Down Expand Up @@ -1089,9 +1110,9 @@ impl<W> ContentLenWrite<W> {
}
}

impl<W> Io for ContentLenWrite<W>
impl<W> ErrorType for ContentLenWrite<W>
where
W: Io,
W: ErrorType,
{
type Error = Error<W::Error>;
}
Expand Down Expand Up @@ -1129,17 +1150,21 @@ impl<W> ChunkedWrite<W> {
where
W: Write,
{
self.output.write_all(b"\r\n").await.map_err(Error::Io)
self.output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)
}

pub fn release(self) -> W {
self.output
}
}

impl<W> Io for ChunkedWrite<W>
impl<W> ErrorType for ChunkedWrite<W>
where
W: Io,
W: ErrorType,
{
type Error = Error<W::Error>;
}
Expand All @@ -1155,12 +1180,18 @@ where
self.output
.write_all(len_str.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

self.output.write_all(buf).await.map_err(Error::Io)?;
self.output
.write_all(buf)
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
self.output
.write_all("\r\n".as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

Ok(buf.len())
Expand Down Expand Up @@ -1389,73 +1420,81 @@ where
let mut written = false;

if !request {
output.write_all(b"HTTP/1.1").await.map_err(Error::Io)?;
output
.write_all(b"HTTP/1.1")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
written = true;
}

if let Some(token) = token {
if written {
output.write_all(b" ").await.map_err(Error::Io)?;
output
.write_all(b" ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
}

output
.write_all(token.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

written = true;
}

if let Some(extra) = extra {
if written {
output.write_all(b" ").await.map_err(Error::Io)?;
output
.write_all(b" ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
}

output
.write_all(extra.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

written = true;
}

if request {
if written {
output.write_all(b" ").await.map_err(Error::Io)?;
output
.write_all(b" ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
}

output.write_all(b"HTTP/1.1").await.map_err(Error::Io)?;
output
.write_all(b"HTTP/1.1")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
}

output.write_all(b"\r\n").await.map_err(Error::Io)?;
output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

Ok(())
}

#[cfg(feature = "embedded-svc")]
mod embedded_svc_compat {
use core::future::Future;
use core::str;

use embedded_io::asynch::Read;
use embedded_svc::http::client::asynch::Method;

use super::Body;

impl<'b, T> embedded_svc::io::asynch::Read for Body<'b, T>
where
T: Read + 'b,
{
type ReadFuture<'a>
= impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
async move { Read::read(self, buf).await }
}
}

impl From<Method> for super::Method {
fn from(method: Method) -> Self {
match method {
Expand Down
Loading

0 comments on commit e9f5e2f

Please sign in to comment.