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

chore: update to embedded-io 0.6 #41

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reqwless"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
resolver = "2"
description = "HTTP client for embedded devices"
Expand All @@ -13,18 +13,18 @@ keywords = ["embedded", "async", "http", "no_std"]
exclude = [".github"]

[dependencies]
buffered-io = { version = "0.3.0", features = ["async"] }
embedded-io = { version = "0.5.0" }
embedded-io-async = { version = "0.5.0" }
embedded-nal-async = "0.5.0"
buffered-io = { version = "0.4.0", features = ["async"] }
embedded-io = { version = "0.6" }
embedded-io-async = { version = "0.6" }
embedded-nal-async = "0.6.0"
httparse = { version = "1.8.0", default-features = false }
heapless = "0.7"
hex = { version = "0.4", default-features = false }
base64 = { version = "0.21.0", default-features = false }
rand_core = { version = "0.6", default-features = true }
log = { version = "0.4", optional = true }
defmt = { version = "0.3", optional = true }
embedded-tls = { version = "0.15", default-features = false, features = [
embedded-tls = { version = "0.16", default-features = false, features = [
"async",
], optional = true }
rand_chacha = { version = "0.3", default-features = false }
Expand All @@ -35,8 +35,8 @@ hyper = { version = "0.14.23", features = ["full"] }
tokio = { version = "1.21.2", features = ["full"] }
tokio-rustls = { version = "0.23.4" }
futures-util = { version = "0.3" }
embedded-io-async = { version = "0.5", features = ["std"] }
embedded-io-adapters = { version = "0.5", features = ["std", "tokio-1"] }
embedded-io-async = { version = "0.6", features = ["std"] }
embedded-io-adapters = { version = "0.6", features = ["std", "tokio-1"] }
rustls-pemfile = "1.0"
env_logger = "0.10"
log = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Before upgrading check that everything is available on all tier1 targets here:
# https://rust-lang.github.io/rustup-components-history
[toolchain]
channel = "nightly-2022-11-22"
channel = "nightly-2023-10-02"
components = ["clippy"]
22 changes: 4 additions & 18 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ where
}

/// Represents a HTTP connection that may be encrypted or unencrypted.
#[allow(clippy::large_enum_variant)]
pub enum HttpConnection<'m, T>
where
T: Read + Write,
Expand Down Expand Up @@ -487,7 +488,7 @@ where
}

mod buffered_io_adapter {
use embedded_io::{Error as _, ErrorType, ReadExactError, WriteAllError};
use embedded_io::{Error as _, ErrorType, ReadExactError};
use embedded_io_async::{Read, Write};

pub struct Error(embedded_io::ErrorKind);
Expand All @@ -504,18 +505,6 @@ mod buffered_io_adapter {
}
}

impl<T> From<WriteAllError<T>> for Error
where
T: embedded_io::Error,
{
fn from(value: WriteAllError<T>) -> Self {
match value {
WriteAllError::WriteZero => Self(embedded_io::ErrorKind::Other),
WriteAllError::Other(e) => Self(e.kind()),
}
}
}

pub struct ConnErrorAdapter<C>(pub C);

impl<C> ErrorType for ConnErrorAdapter<C> {
Expand All @@ -534,11 +523,8 @@ mod buffered_io_adapter {
self.0.flush().await.map_err(|e| Error(e.kind()))
}

async fn write_all(&mut self, buf: &[u8]) -> Result<(), embedded_io::WriteAllError<Self::Error>> {
self.0.write_all(buf).await.map_err(|e| match e {
WriteAllError::WriteZero => WriteAllError::WriteZero,
WriteAllError::Other(e) => WriteAllError::Other(Error(e.kind())),
})
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.0.write_all(buf).await.map_err(|e| Error(e.kind()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ impl<'a> TryFrom<&'a [u8]> for KeepAlive {
timeout: None,
max: None,
};
for part in core::str::from_utf8(from)?.split(",") {
let mut splitted = part.split("=").map(|s| s.trim());
for part in core::str::from_utf8(from)?.split(',') {
let mut splitted = part.split('=').map(|s| s.trim());
if let (Some(key), Some(value)) = (splitted.next(), splitted.next()) {
match key {
_ if key.eq_ignore_ascii_case("timeout") => keep_alive.timeout = value.parse().ok(),
Expand Down
15 changes: 1 addition & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![cfg_attr(not(test), no_std)]
#![feature(impl_trait_projections)]
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
#![doc = include_str!("../README.md")]
use core::{num::ParseIntError, str::Utf8Error};

use embedded_io_async::{ReadExactError, WriteAllError};
use embedded_io_async::ReadExactError;

mod fmt;

Expand Down Expand Up @@ -61,18 +60,6 @@ impl<E: embedded_io::Error> From<ReadExactError<E>> for Error {
}
}

impl<E> From<WriteAllError<E>> for Error
where
E: embedded_io::Error,
{
fn from(value: WriteAllError<E>) -> Self {
match value {
WriteAllError::WriteZero => Error::Network(embedded_io::ErrorKind::Other),
WriteAllError::Other(e) => Error::Network(e.kind()),
}
}
}

#[cfg(feature = "embedded-tls")]
impl From<embedded_tls::TlsError> for Error {
fn from(e: embedded_tls::TlsError) -> Error {
Expand Down
27 changes: 12 additions & 15 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::Error;
use core::fmt::Write as _;
use core::mem::size_of;
use embedded_io::{Error as _, ErrorType};
use embedded_io_async::{Write, WriteAllError};
use embedded_io_async::Write;
use heapless::String;

/// A read only HTTP request type
Expand Down Expand Up @@ -169,7 +169,7 @@ where
Some(len) => {
trace!("Writing not-chunked body");
let mut writer = FixedBodyWriter(c, 0);
body.write(&mut writer).await?;
body.write(&mut writer).await.map_err(to_errorkind)?;

if writer.1 != len {
return Err(Error::IncorrectBodyWritten);
Expand Down Expand Up @@ -273,7 +273,7 @@ impl Method {
}

async fn write_str<C: Write>(c: &mut C, data: &str) -> Result<(), Error> {
c.write_all(data.as_bytes()).await?;
c.write_all(data.as_bytes()).await.map_err(to_errorkind)?;
Ok(())
}

Expand All @@ -297,15 +297,15 @@ pub trait RequestBody {
}

/// Write the body to the provided writer
async fn write<W: Write>(&self, writer: &mut W) -> Result<(), WriteAllError<W::Error>>;
async fn write<W: Write>(&self, writer: &mut W) -> Result<(), W::Error>;
}

impl RequestBody for () {
fn len(&self) -> Option<usize> {
None
}

async fn write<W: Write>(&self, _writer: &mut W) -> Result<(), WriteAllError<W::Error>> {
async fn write<W: Write>(&self, _writer: &mut W) -> Result<(), W::Error> {
Ok(())
}
}
Expand All @@ -315,7 +315,7 @@ impl RequestBody for &[u8] {
Some(<[u8]>::len(self))
}

async fn write<W: Write>(&self, writer: &mut W) -> Result<(), WriteAllError<W::Error>> {
async fn write<W: Write>(&self, writer: &mut W) -> Result<(), W::Error> {
writer.write_all(self).await
}
}
Expand All @@ -328,7 +328,7 @@ where
self.as_ref().map(|inner| inner.len()).unwrap_or_default()
}

async fn write<W: Write>(&self, writer: &mut W) -> Result<(), WriteAllError<W::Error>> {
async fn write<W: Write>(&self, writer: &mut W) -> Result<(), W::Error> {
if let Some(inner) = self.as_ref() {
inner.write(writer).await
} else {
Expand Down Expand Up @@ -356,7 +356,7 @@ where
Ok(written)
}

async fn write_all(&mut self, buf: &[u8]) -> Result<(), WriteAllError<Self::Error>> {
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.0.write_all(buf).await?;
self.1 += buf.len();
Ok(())
Expand All @@ -376,11 +376,8 @@ where
type Error = embedded_io::ErrorKind;
}

fn to_errorkind<E: embedded_io::Error>(e: WriteAllError<E>) -> embedded_io::ErrorKind {
match e {
WriteAllError::WriteZero => embedded_io::ErrorKind::Other,
WriteAllError::Other(e) => e.kind(),
}
fn to_errorkind<E: embedded_io::Error>(e: E) -> embedded_io::ErrorKind {
e.kind()
}

impl<C> Write for ChunkedBodyWriter<'_, C>
Expand All @@ -392,7 +389,7 @@ where
Ok(buf.len())
}

async fn write_all(&mut self, buf: &[u8]) -> Result<(), WriteAllError<Self::Error>> {
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
// Write chunk header
let len = buf.len();
let mut hex = [0; 2 * size_of::<usize>()];
Expand Down Expand Up @@ -469,7 +466,7 @@ mod tests {
None // Unknown length: triggers chunked body
}

async fn write<W: Write>(&self, writer: &mut W) -> Result<(), WriteAllError<W::Error>> {
async fn write<W: Write>(&self, writer: &mut W) -> Result<(), W::Error> {
writer.write_all(self.0).await
}
}
Expand Down
1 change: 0 additions & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ where
if parse_status.is_complete() {
header_len = parse_status.unwrap();
break;
} else {
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(async_fn_in_trait)]
#![feature(impl_trait_projections)]
#![allow(incomplete_features)]
use embedded_io_adapters::tokio_1::FromTokio;
use embedded_nal_async::{AddrType, IpAddr, Ipv4Addr};
Expand Down Expand Up @@ -286,7 +285,10 @@ impl embedded_nal_async::TcpConnect for TokioTcp {
type Error = std::io::Error;
type Connection<'m> = FromTokio<TcpStream>;

async fn connect<'m>(&self, remote: embedded_nal_async::SocketAddr) -> Result<Self::Connection<'m>, Self::Error> {
async fn connect<'m>(&'m self, remote: embedded_nal_async::SocketAddr) -> Result<Self::Connection<'m>, Self::Error>
where
Self: 'm,
{
let ip = match remote {
embedded_nal_async::SocketAddr::V4(a) => a.ip().octets().into(),
embedded_nal_async::SocketAddr::V6(a) => a.ip().octets().into(),
Expand Down