Skip to content

Commit

Permalink
Merge branch 'mxinden/master' into parts
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Jan 31, 2021
2 parents 43dde94 + f270fb7 commit 61be062
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 139 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
107 changes: 107 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
on: [push, pull_request]

name: Continuous integration

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

# Caching
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: cargo-registry-${{ hashFiles('Cargo.toml') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: cargo-index-${{ hashFiles('Cargo.toml') }}

- uses: actions-rs/cargo@v1
with:
command: check
args: --all --all-features

test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

# Caching
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: cargo-registry-${{ hashFiles('Cargo.toml') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: cargo-index-${{ hashFiles('Cargo.toml') }}

- uses: actions-rs/cargo@v1
with:
command: test
args: --all --all-features

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy

# Caching
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: cargo-registry-${{ hashFiles('Cargo.toml') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: cargo-index-${{ hashFiles('Cargo.toml') }}

- uses: actions-rs/cargo@v1
continue-on-error: true
with:
command: clippy
args: -- -D warnings
30 changes: 17 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "futures_codec"
name = "asynchronous-codec"
edition = "2018"
version = "0.4.1"
authors = ["Matt Hunzinger <[email protected]>"]
version = "0.5.0"
authors = ["Max Inden <[email protected]>"]
description = "Utilities for encoding and decoding frames using `async/await`"
license = "MIT"
readme = "README.md"
repository = "https://github.com/matthunz/futures-codec"
homepage = "https://github.com/matthunz/futures-codec"
documentation = "https://docs.rs/crate/futures_codec"
repository = "https://github.com/mxinden/asynchronous-codec"
homepage = "https://github.com/mxinden/asynchronous-codec"
documentation = "https://docs.rs/crate/asynchronous-codec"
keywords = ["future", "futures", "async", "codec"]
categories = ["asynchronous", "network-programming"]

Expand All @@ -18,22 +18,26 @@ json = [ "serde", "serde_json" ]
cbor = [ "serde", "serde_cbor" ]

[dependencies]
bytes = "0.5.4"
futures = "0.3.5"
memchr = "2.2.3"
pin-project = "0.4.22"
bytes = "1"
futures-sink = "0.3"
futures-util = { version = "0.3", features = ["io"] }
memchr = "2"
pin-project-lite = "0.2"

[dev-dependencies]
futures = "0.3"

[dependencies.serde]
version = '1.0.113'
version = "1"
optional = true
features = [ "derive" ]

[dependencies.serde_json]
version = '1.0.55'
version = "1"
optional = true

[dependencies.serde_cbor]
version = '0.11.1'
version = "0.11"
optional = true

[package.metadata.docs.rs]
Expand Down
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2019 Matt Hunzinger
Copyright (c) 2019 - 2020 Matt Hunzinger
Copyright (c) 2021 Max Inden

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# futures_codec
# Asynchronous Codec

Utilities for encoding and decoding frames using async/await.

This is a fork of [`futures-codec`](https://github.com/matthunz/futures-codec)
by [Matt Hunzinger](https://github.com/matthunz) borrowing many concepts from
[`tokio-codec`](https://crates.io/crates/tokio-codec).

Contains adapters to go from streams of bytes, `AsyncRead` and `AsyncWrite`,
to framed streams implementing `Sink` and `Stream`. Framed streams are also known as transports.

[![Latest Version](https://img.shields.io/crates/v/futures-codec.svg)](https://crates.io/crates/futures-codec)
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/futures-codec)
[![Build Status](https://travis-ci.com/matthunz/futures-codec.svg)](https://travis-ci.com/matthunz/futures-codec)
[![Latest Version](https://img.shields.io/crates/v/asynchronous-codec.svg)](https://crates.io/crates/asynchronous-codec)
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/asynchronous-codec)
![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)


### Example

```rust
use futures_codec::{LinesCodec, Framed};
use asynchronous_codec::{LinesCodec, Framed};

async fn main() {
// let stream = ...
Expand Down
5 changes: 2 additions & 3 deletions benches/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

extern crate test;

use futures::{executor, TryStreamExt};
use futures_codec::{FramedRead, LinesCodec};
use std::io::Cursor;
use asynchronous_codec::{FramedRead, LinesCodec};
use futures::{executor, io::Cursor, TryStreamExt};

#[bench]
fn short(b: &mut test::Bencher) {
Expand Down
2 changes: 1 addition & 1 deletion src/codec/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::io::Error;
/// use bytes::Bytes;
/// use futures::{SinkExt, TryStreamExt};
/// use futures::io::Cursor;
/// use futures_codec::{BytesCodec, Framed};
/// use asynchronous_codec::{BytesCodec, Framed};
///
/// let mut buf = vec![];
/// // Cursor implements AsyncRead and AsyncWrite
Expand Down
36 changes: 32 additions & 4 deletions src/codec/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde_cbor::Error as CborError;
/// # use futures::{executor, SinkExt, TryStreamExt};
/// # use futures::io::Cursor;
/// use serde::{Serialize, Deserialize};
/// use futures_codec::{CborCodec, Framed};
/// use asynchronous_codec::{CborCodec, Framed};
///
/// #[derive(Serialize, Deserialize)]
/// struct Something {
Expand Down Expand Up @@ -47,15 +47,33 @@ pub enum CborCodecError {
Cbor(CborError),
}

impl std::fmt::Display for CborCodecError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CborCodecError::Io(e) => write!(f, "I/O error: {}", e),
CborCodecError::Cbor(e) => write!(f, "CBOR error: {}", e),
}
}
}

impl std::error::Error for CborCodecError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CborCodecError::Io(ref e) => Some(e),
CborCodecError::Cbor(ref e) => Some(e),
}
}
}

impl From<IoError> for CborCodecError {
fn from(e: IoError) -> CborCodecError {
return CborCodecError::Io(e);
CborCodecError::Io(e)
}
}

impl From<CborError> for CborCodecError {
fn from(e: CborError) -> CborCodecError {
return CborCodecError::Cbor(e);
CborCodecError::Cbor(e)
}
}

Expand Down Expand Up @@ -138,6 +156,16 @@ where
}
}

impl<Enc, Dec> Default for CborCodec<Enc, Dec>
where
for<'de> Dec: Deserialize<'de> + 'static,
for<'de> Enc: Serialize + 'static,
{
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod test {
use bytes::BytesMut;
Expand Down Expand Up @@ -180,7 +208,7 @@ mod test {
name: "Test name".to_owned(),
data: 34,
};
codec.encode(item1.clone(), &mut buff).unwrap();
codec.encode(item1, &mut buff).unwrap();

let mut start = buff.clone().split_to(4);
assert_eq!(codec.decode(&mut start).unwrap(), None);
Expand Down
37 changes: 32 additions & 5 deletions src/codec/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use crate::{Decoder, Encoder};
use bytes::{Buf, BufMut, BytesMut};

use serde::{Deserialize, Serialize};
use serde_json;

/// A codec for JSON encoding and decoding using serde_json
/// Enc is the type to encode, Dec is the type to decode
/// ```
/// # use futures::{executor, SinkExt, TryStreamExt};
/// # use futures::io::Cursor;
/// use serde::{Serialize, Deserialize};
/// use futures_codec::{JsonCodec, Framed};
/// use asynchronous_codec::{JsonCodec, Framed};
///
/// #[derive(Serialize, Deserialize)]
/// struct Something {
Expand Down Expand Up @@ -46,15 +45,33 @@ pub enum JsonCodecError {
Json(serde_json::Error),
}

impl std::fmt::Display for JsonCodecError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JsonCodecError::Io(e) => write!(f, "I/O error: {}", e),
JsonCodecError::Json(e) => write!(f, "JSON error: {}", e),
}
}
}

impl std::error::Error for JsonCodecError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
JsonCodecError::Io(ref e) => Some(e),
JsonCodecError::Json(ref e) => Some(e),
}
}
}

impl From<std::io::Error> for JsonCodecError {
fn from(e: std::io::Error) -> JsonCodecError {
return JsonCodecError::Io(e);
JsonCodecError::Io(e)
}
}

impl From<serde_json::Error> for JsonCodecError {
fn from(e: serde_json::Error) -> JsonCodecError {
return JsonCodecError::Json(e);
JsonCodecError::Json(e)
}
}

Expand Down Expand Up @@ -136,6 +153,16 @@ where
}
}

impl<Enc, Dec> Default for JsonCodec<Enc, Dec>
where
for<'de> Dec: Deserialize<'de> + 'static,
for<'de> Enc: Serialize + 'static,
{
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod test {
use bytes::BytesMut;
Expand Down Expand Up @@ -178,7 +205,7 @@ mod test {
name: "Test name".to_owned(),
data: 34,
};
codec.encode(item1.clone(), &mut buff).unwrap();
codec.encode(item1, &mut buff).unwrap();

let mut start = buff.clone().split_to(4);
assert_eq!(codec.decode(&mut start).unwrap(), None);
Expand Down
2 changes: 1 addition & 1 deletion src/codec/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const U64_LENGTH: usize = std::mem::size_of::<u64>();
/// This codec will most likely be used wrapped in another codec like so.
///
/// ```
/// use futures_codec::{Decoder, Encoder, LengthCodec};
/// use asynchronous_codec::{Decoder, Encoder, LengthCodec};
/// use bytes::{Bytes, BytesMut};
/// use std::io::{Error, ErrorKind};
///
Expand Down
2 changes: 1 addition & 1 deletion src/codec/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::{Error, ErrorKind};
/// ```rust
/// # futures::executor::block_on(async move {
/// use futures::stream::TryStreamExt; // for lines.try_next()
/// use futures_codec::{FramedRead, LinesCodec};
/// use asynchronous_codec::{FramedRead, LinesCodec};
///
/// let input = "hello\nworld\nthis\nis\ndog\n".as_bytes();
/// let mut lines = FramedRead::new(input, LinesCodec);
Expand Down
Loading

0 comments on commit 61be062

Please sign in to comment.