-
-
Save Nemo157/4a6212cdda5a8086119e044a9ed7e9e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(generators)] | |
use futures::io::{AsyncReadExt, AsyncRead}; | |
pub fn decode(input: impl AsyncRead) -> impl AsyncRead { | |
async_io_macros::async_read! { | |
futures::pin_mut!(input); | |
loop { | |
let mut bytes = [0; 4]; | |
let len = input.read(&mut bytes).await?; | |
if len == 0 { | |
break; | |
} | |
input.read_exact(&mut bytes[len..]).await?; | |
for byte in &mut bytes { | |
*byte = match *byte { | |
b'A'..=b'Z' => *byte - b'A', | |
b'a'..=b'z' => *byte - b'a' + 26, | |
b'0'..=b'9' => *byte - b'0' + 52, | |
b'+' | b'-' => 62, | |
b'/' | b',' | b'_' => 63, | |
b'=' => b'=', | |
_ => return Err(std::io::Error::new(std::io::ErrorKind::Other, "invalid char")), | |
} | |
} | |
let out = [ | |
bytes[0] << 2 | bytes[1] >> 4, | |
bytes[1] << 4 | bytes[2] >> 2, | |
bytes[2] << 6 | bytes[3], | |
]; | |
let mut out = if bytes[2] == b'=' { &out[..1] } else if bytes[3] == b'=' { &out[..2] } else { &out[..] }; | |
while !out.is_empty() { | |
yield |buffer| { | |
let len = buffer.len().min(out.len()); | |
buffer[..len].copy_from_slice(&out[..len]); | |
let (_, tail) = out.split_at(len); | |
out = tail; | |
Ok(len) | |
}; | |
} | |
} | |
Ok(()) | |
} | |
} | |
#[test] | |
fn test() { | |
let input = b"SGVsbG8gRmVycmlzCg=="; | |
let decoded = decode(&input[..]); | |
futures::pin_mut!(decoded); | |
let mut buffer = Vec::new(); | |
futures::executor::block_on(decoded.read_to_end(&mut buffer)).unwrap(); | |
assert_eq!(buffer, b"Hello Ferris\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is automatically @generated by Cargo. | |
# It is not intended for manual editing. | |
version = 3 | |
[[package]] | |
name = "async-io-macros" | |
version = "0.1.0" | |
source = "git+https://github.com/Nemo157/async-io-macros#5dd57d6cbe831bc7ca5b9462bfe0ed6c9c9a7979" | |
dependencies = [ | |
"async-io-macros-impl", | |
"futures-core", | |
"futures-io", | |
"pin-project-lite 0.1.11", | |
] | |
[[package]] | |
name = "async-io-macros-impl" | |
version = "0.1.0" | |
source = "git+https://github.com/Nemo157/async-io-macros#5dd57d6cbe831bc7ca5b9462bfe0ed6c9c9a7979" | |
dependencies = [ | |
"proc-macro2", | |
"quote", | |
"syn", | |
] | |
[[package]] | |
name = "foo" | |
version = "0.1.0" | |
dependencies = [ | |
"async-io-macros", | |
"futures", | |
] | |
[[package]] | |
name = "futures" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "da9052a1a50244d8d5aa9bf55cbc2fb6f357c86cc52e46c62ed390a7180cf150" | |
dependencies = [ | |
"futures-channel", | |
"futures-core", | |
"futures-executor", | |
"futures-io", | |
"futures-sink", | |
"futures-task", | |
"futures-util", | |
] | |
[[package]] | |
name = "futures-channel" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "f2d31b7ec7efab6eefc7c57233bb10b847986139d88cc2f5a02a1ae6871a1846" | |
dependencies = [ | |
"futures-core", | |
"futures-sink", | |
] | |
[[package]] | |
name = "futures-core" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "79e5145dde8da7d1b3892dad07a9c98fc04bc39892b1ecc9692cf53e2b780a65" | |
[[package]] | |
name = "futures-executor" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e9e59fdc009a4b3096bf94f740a0f2424c082521f20a9b08c5c07c48d90fd9b9" | |
dependencies = [ | |
"futures-core", | |
"futures-task", | |
"futures-util", | |
] | |
[[package]] | |
name = "futures-io" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500" | |
[[package]] | |
name = "futures-macro" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c287d25add322d9f9abdcdc5927ca398917996600182178774032e9f8258fedd" | |
dependencies = [ | |
"proc-macro-hack", | |
"proc-macro2", | |
"quote", | |
"syn", | |
] | |
[[package]] | |
name = "futures-sink" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "caf5c69029bda2e743fddd0582d1083951d65cc9539aebf8812f36c3491342d6" | |
[[package]] | |
name = "futures-task" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "13de07eb8ea81ae445aca7b69f5f7bf15d7bf4912d8ca37d6645c77ae8a58d86" | |
dependencies = [ | |
"once_cell", | |
] | |
[[package]] | |
name = "futures-util" | |
version = "0.3.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "632a8cd0f2a4b3fdea1657f08bde063848c3bd00f9bbf6e256b8be78802e624b" | |
dependencies = [ | |
"futures-channel", | |
"futures-core", | |
"futures-io", | |
"futures-macro", | |
"futures-sink", | |
"futures-task", | |
"memchr", | |
"pin-project-lite 0.2.4", | |
"pin-utils", | |
"proc-macro-hack", | |
"proc-macro-nested", | |
"slab", | |
] | |
[[package]] | |
name = "memchr" | |
version = "2.3.4" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" | |
[[package]] | |
name = "once_cell" | |
version = "1.5.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" | |
[[package]] | |
name = "pin-project-lite" | |
version = "0.1.11" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" | |
[[package]] | |
name = "pin-project-lite" | |
version = "0.2.4" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827" | |
[[package]] | |
name = "pin-utils" | |
version = "0.1.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" | |
[[package]] | |
name = "proc-macro-hack" | |
version = "0.5.19" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" | |
[[package]] | |
name = "proc-macro-nested" | |
version = "0.1.7" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" | |
[[package]] | |
name = "proc-macro2" | |
version = "1.0.24" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" | |
dependencies = [ | |
"unicode-xid", | |
] | |
[[package]] | |
name = "quote" | |
version = "1.0.9" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" | |
dependencies = [ | |
"proc-macro2", | |
] | |
[[package]] | |
name = "slab" | |
version = "0.4.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" | |
[[package]] | |
name = "syn" | |
version = "1.0.60" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" | |
dependencies = [ | |
"proc-macro2", | |
"quote", | |
"unicode-xid", | |
] | |
[[package]] | |
name = "unicode-xid" | |
version = "0.2.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "foo" | |
version = "0.1.0" | |
authors = ["Wim Looman <git@nemo157.com>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[lib] | |
path = "_lib.rs" | |
[dependencies] | |
async-io-macros = { git = "https://github.com/Nemo157/async-io-macros" } | |
futures = "0.3.12" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment