smol'er
This commit is contained in:
parent
a7fafcff22
commit
f879ff30d2
84
Cargo.lock
generated
84
Cargo.lock
generated
@ -353,48 +353,12 @@ version = "2.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
|
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
|
|
||||||
dependencies = [
|
|
||||||
"futures-channel",
|
|
||||||
"futures-core",
|
|
||||||
"futures-executor",
|
|
||||||
"futures-io",
|
|
||||||
"futures-sink",
|
|
||||||
"futures-task",
|
|
||||||
"futures-util",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-channel"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
|
|
||||||
dependencies = [
|
|
||||||
"futures-core",
|
|
||||||
"futures-sink",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-core"
|
name = "futures-core"
|
||||||
version = "0.3.30"
|
version = "0.3.30"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-executor"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
|
|
||||||
dependencies = [
|
|
||||||
"futures-core",
|
|
||||||
"futures-task",
|
|
||||||
"futures-util",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-io"
|
name = "futures-io"
|
||||||
version = "0.3.30"
|
version = "0.3.30"
|
||||||
@ -429,47 +393,6 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-macro"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-sink"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-task"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-util"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
|
|
||||||
dependencies = [
|
|
||||||
"futures-channel",
|
|
||||||
"futures-core",
|
|
||||||
"futures-io",
|
|
||||||
"futures-macro",
|
|
||||||
"futures-sink",
|
|
||||||
"futures-task",
|
|
||||||
"memchr",
|
|
||||||
"pin-project-lite",
|
|
||||||
"pin-utils",
|
|
||||||
"slab",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gimli"
|
name = "gimli"
|
||||||
version = "0.28.1"
|
version = "0.28.1"
|
||||||
@ -651,12 +574,6 @@ version = "0.2.13"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
|
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pin-utils"
|
|
||||||
version = "0.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "piper"
|
name = "piper"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
@ -711,7 +628,6 @@ dependencies = [
|
|||||||
name = "protohacke-rs"
|
name = "protohacke-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
|
||||||
"miette",
|
"miette",
|
||||||
"miniserde",
|
"miniserde",
|
||||||
"smol",
|
"smol",
|
||||||
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.3.30"
|
|
||||||
miette = { version = "5", features = ["fancy"] }
|
miette = { version = "5", features = ["fancy"] }
|
||||||
miniserde = "0.1.38"
|
miniserde = "0.1.38"
|
||||||
smol = "1.3.0"
|
smol = "1.3.0"
|
||||||
|
36
src/main.rs
36
src/main.rs
@ -1,9 +1,9 @@
|
|||||||
use futures::{
|
|
||||||
future::ready, io::BufReader, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, StreamExt,
|
|
||||||
TryStreamExt,
|
|
||||||
};
|
|
||||||
use miette::IntoDiagnostic;
|
use miette::IntoDiagnostic;
|
||||||
use miniserde::{json::Number, Deserialize, Serialize};
|
use miniserde::{json::Number, Deserialize, Serialize};
|
||||||
|
use smol::{
|
||||||
|
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
|
||||||
|
stream::StreamExt,
|
||||||
|
};
|
||||||
|
|
||||||
fn pow(a: u64, d: u64, n: u64) -> u64 {
|
fn pow(a: u64, d: u64, n: u64) -> u64 {
|
||||||
let n: u128 = n.into();
|
let n: u128 = n.into();
|
||||||
@ -119,10 +119,7 @@ struct Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn json_prime(number: Number) -> bool {
|
fn json_prime(number: Number) -> bool {
|
||||||
match number {
|
matches!(number, Number::U64(n) if is_prime(n))
|
||||||
Number::U64(n) => is_prime(n),
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_process(input: String) -> miniserde::Result<String> {
|
fn try_process(input: String) -> miniserde::Result<String> {
|
||||||
@ -142,20 +139,20 @@ fn process(input: String) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle(stream: smol::io::Result<smol::net::TcpStream>) -> smol::io::Result<()> {
|
async fn handle(stream: smol::io::Result<smol::net::TcpStream>) -> smol::io::Result<()> {
|
||||||
let (reader, writer) = stream?.split();
|
let (reader, mut writer) = smol::io::split(stream?);
|
||||||
let mut alive = true;
|
let mut alive = true;
|
||||||
BufReader::new(reader)
|
let mut items = BufReader::new(reader)
|
||||||
.lines()
|
.lines()
|
||||||
.map_ok(process)
|
.map(|r| r.map(process))
|
||||||
.take_while(|item| {
|
.take_while(|item| match item {
|
||||||
ready(match item {
|
|
||||||
Ok(s) if s.is_empty() => std::mem::take(&mut alive),
|
Ok(s) if s.is_empty() => std::mem::take(&mut alive),
|
||||||
_ => alive,
|
_ => alive,
|
||||||
})
|
})
|
||||||
})
|
.map(|r| r.map(|s| s + "\n"));
|
||||||
.map_ok(|s| s + "\n")
|
while let Some(item) = items.next().await.transpose()? {
|
||||||
.forward(writer.into_sink::<String>())
|
writer.write_all(item.as_bytes()).await?;
|
||||||
.await
|
}
|
||||||
|
writer.close().await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> miette::Result<()> {
|
fn main() -> miette::Result<()> {
|
||||||
@ -164,11 +161,14 @@ fn main() -> miette::Result<()> {
|
|||||||
.await
|
.await
|
||||||
.into_diagnostic()?
|
.into_diagnostic()?
|
||||||
.incoming()
|
.incoming()
|
||||||
.for_each_concurrent(None, |stream| async {
|
.for_each(|stream| {
|
||||||
|
smol::spawn(async {
|
||||||
if let Err(e) = handle(stream).await.into_diagnostic() {
|
if let Err(e) = handle(stream).await.into_diagnostic() {
|
||||||
eprintln!("{e}");
|
eprintln!("{e}");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.detach()
|
||||||
|
})
|
||||||
.await;
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user