Skip to content

Commit e589ff0

Browse files
committed
feat(ci): clippy and tests
fix(ci): Rust 1.70.0 compatibility
1 parent a7f5b05 commit e589ff0

File tree

12 files changed

+131
-73
lines changed

12 files changed

+131
-73
lines changed

.github/workflows/rust.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
13+
pre_ci:
14+
uses: dtolnay/.github/.github/workflows/pre_ci.yml@master
15+
1316
build:
1417
name: Build - ${{ matrix.platform.target }}
1518
runs-on: ubuntu-24.04
@@ -42,7 +45,7 @@ jobs:
4245
args: "--release --verbose --features feat-rate-limit"
4346

4447
test:
45-
name: Test
48+
name: Test 1.70.0
4649
runs-on: ubuntu-24.04
4750
steps:
4851
- name: Checkout
@@ -52,3 +55,27 @@ jobs:
5255
toolchain: 1.70.0
5356
- name: Run tests
5457
run: cargo test --release --verbose --features feat-rate-limit
58+
59+
clippy:
60+
name: Clippy
61+
runs-on: ubuntu-latest
62+
if: github.event_name != 'pull_request'
63+
timeout-minutes: 45
64+
steps:
65+
- uses: actions/checkout@v4
66+
- uses: dtolnay/rust-toolchain@clippy
67+
- run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic
68+
69+
doc:
70+
name: Documentation
71+
needs: pre_ci
72+
if: needs.pre_ci.outputs.continue
73+
runs-on: ubuntu-latest
74+
timeout-minutes: 45
75+
env:
76+
RUSTDOCFLAGS: -Dwarnings
77+
steps:
78+
- uses: actions/checkout@v4
79+
- uses: dtolnay/rust-toolchain@nightly
80+
- uses: dtolnay/install@cargo-docs-rs
81+
- run: cargo docs-rs

Cargo.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ repository = "https://github.com/hanyu-dev/tokio-splice2"
1414

1515
[package.metadata.docs.rs]
1616
features = ["feat-rate-limit"]
17-
targets = ["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]
17+
targets = ["x86_64-unknown-linux-gnu"]
18+
rustdoc-args = [
19+
"--generate-link-to-definition",
20+
"--extern-html-root-url=core=https://doc.rust-lang.org",
21+
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
22+
"--extern-html-root-url=std=https://doc.rust-lang.org",
23+
]
1824

1925
[dependencies]
2026
crossbeam-utils = "0.8.21"
2127
pin-project = "1.1.10"
2228
rustix = { version = "1.0.0", features = ["pipe"] }
23-
tokio = { version = "1.46.0", features = ["fs", "net"] }
29+
tokio = { version = "1.47.1", features = ["fs", "net"] }
2430
tracing = { version = "0.1.41", optional = true }
2531

2632
[dev-dependencies]
27-
human-format-next = "0.2.2"
28-
rand = "0.9.1"
29-
tokio = { version = "1.46.0", features = ["net", "macros", "rt", "rt-multi-thread", "signal", "io-util", "time"] }
33+
rand = "0.9.2"
34+
tokio = { version = "1.47.1", features = ["net", "macros", "rt", "rt-multi-thread", "signal", "io-util", "time"] }
3035
tracing-appender = { version = "0.2.3", features = ["parking_lot"] }
3136
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "parking_lot"] }
3237

examples/proxy.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
//! Example: simple L4 proxy
22
3-
use std::num::NonZeroU64;
43
use std::{env, io};
54

65
use tokio::net::{TcpListener, TcpStream};
7-
#[cfg(feature = "feat-rate-limit")]
8-
use tokio_splice2::rate::RateLimit;
96

107
// #[tokio::main(flavor = "current_thread")]
118
#[tokio::main]
@@ -91,19 +88,14 @@ async fn forwarding(mut stream1: TcpStream) -> io::Result<()> {
9188
// let result = tokio_splice::zero_copy_bidirectional(&mut stream1, &mut
9289
// stream2).await;
9390

94-
let limit = env::var("LIMIT")
95-
.ok()
96-
.and_then(|r| r.parse().ok())
97-
.unwrap_or(1u64 * 1000 * 1000);
91+
// let limit = env::var("LIMIT")
92+
// .ok()
93+
// .and_then(|r| r.parse().ok())
94+
// .unwrap_or(1u64 * 1000 * 1000);
9895

99-
println!(
100-
"Rate limit is set to {}/s",
101-
human_format_next::Formatter::SI
102-
.with_custom_unit("B")
103-
.format(limit),
104-
);
96+
// println!("Rate limit is set to {limit} B/s");
10597

106-
let limit = RateLimit::new(NonZeroU64::new(limit).unwrap());
98+
// let limit = RateLimit::new(NonZeroU64::new(limit).unwrap());
10799

108100
// let rate_limiter_clone = rate_limiter.clone();
109101
// tokio::spawn(async move {
@@ -141,12 +133,8 @@ async fn forwarding(mut stream1: TcpStream) -> io::Result<()> {
141133
// }
142134
// });
143135

144-
let io_sl2sr = tokio_splice2::context::SpliceIoCtx::prepare()?
145-
.into_io()
146-
.with_rate_limit(limit.clone());
147-
let io_sr2sl = tokio_splice2::context::SpliceIoCtx::prepare()?
148-
.into_io()
149-
.with_rate_limit(limit);
136+
let io_sl2sr = tokio_splice2::context::SpliceIoCtx::prepare()?.into_io();
137+
let io_sr2sl = tokio_splice2::context::SpliceIoCtx::prepare()?.into_io();
150138

151139
let traffic = tokio_splice2::io::SpliceBidiIo { io_sl2sr, io_sr2sl }
152140
.execute(&mut stream1, &mut stream2)
@@ -157,17 +145,9 @@ async fn forwarding(mut stream1: TcpStream) -> io::Result<()> {
157145
// let total = traffic.0 + traffic.1;
158146
let cost = instant.elapsed();
159147
println!(
160-
"Forwarded traffic: total: {}, time: {:.2}s, avg: {:.4} -> {}, error: {:?}",
161-
human_format_next::Formatter::SI
162-
.with_custom_unit("B")
163-
.with_decimals::<4>()
164-
.format(total as f64),
148+
"Forwarded traffic: total: {total} B, time: {:.2} s, avg: {:.4} B/s, error: {:?}",
165149
cost.as_secs_f64(),
166150
total as f64 / cost.as_secs_f64(),
167-
human_format_next::Formatter::SI
168-
.with_custom_unit("B/s")
169-
.with_decimals::<4>()
170-
.format((total as f64 / cost.as_secs_f64()) as u64),
171151
traffic.error
172152
);
173153

src/context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl<R, W> SpliceIoCtx<R, W> {
8181
Self::_prepare()
8282
}
8383

84+
#[must_use]
8485
#[inline]
8586
/// Set the target length of bytes to be copy from `R` to `W`.
8687
///
@@ -173,25 +174,36 @@ impl<R, W> SpliceIoCtx<R, W> {
173174
/// Set the pipe size.
174175
///
175176
/// See [`Pipe`]'s top level docs for more details.
177+
///
178+
/// ## Errors
179+
///
180+
/// * Set pipe size failed.
181+
///
182+
/// For more details, see [`fcntl(2)`].
183+
///
184+
/// [`fcntl(2)`]: https://man7.org/linux/man-pages/man2/fcntl.2.html.
176185
pub fn set_pipe_size(mut self, pipe_size: usize) -> io::Result<Self> {
177186
self.pipe.set_pipe_size(pipe_size)?;
178187
Ok(self)
179188
}
180189
}
181190

182191
impl<R, W> SpliceIoCtx<R, W> {
192+
#[must_use]
183193
#[inline]
184194
/// Returns bytes that have been read from `R`.
185195
pub const fn has_read(&self) -> usize {
186196
self.has_read
187197
}
188198

199+
#[must_use]
189200
#[inline]
190201
/// Returns bytes that have been written to `W`.
191202
pub const fn has_written(&self) -> usize {
192203
self.has_written
193204
}
194205

206+
#[must_use]
195207
#[inline]
196208
/// Returns the pipe size.
197209
pub const fn pipe_size(&self) -> NonZeroUsize {
@@ -216,6 +228,7 @@ impl<R, W> SpliceIoCtx<R, W> {
216228
self.pipe.splice_drain_finished() && self.pipe.splice_pump_finished()
217229
}
218230

231+
#[must_use]
219232
#[inline]
220233
/// Returns the traffic result (client TX one).
221234
pub const fn traffic_client_tx(&self, error: Option<io::Error>) -> TrafficResult {
@@ -226,6 +239,7 @@ impl<R, W> SpliceIoCtx<R, W> {
226239
}
227240
}
228241

242+
#[must_use]
229243
#[inline]
230244
/// Returns the traffic result (client RX one).
231245
pub const fn traffic_client_rx(&self, error: Option<io::Error>) -> TrafficResult {
@@ -236,6 +250,7 @@ impl<R, W> SpliceIoCtx<R, W> {
236250
}
237251
}
238252

253+
#[must_use]
239254
#[inline]
240255
/// Builder pattern version of [`SpliceIo::new`].
241256
pub fn into_io(self) -> SpliceIo<R, W> {

src/io.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl<R, W, const RATE_LIMITER_IS_ENABLED: bool> ops::Deref
5858
}
5959

6060
impl<R, W> SpliceIo<R, W, RATE_LIMITER_DISABLED> {
61+
#[must_use]
6162
/// Create a new `SpliceIo` instance with ctx and pinned `R` / `W`.
6263
pub fn new(ctx: SpliceIoCtx<R, W>) -> Self {
6364
SpliceIo {

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub use rate::RateLimit;
8282
///
8383
/// See [`SpliceIoCtx::prepare`] and [`SpliceIo::execute`] for more details; see
8484
/// the [crate-level documentation](crate) for known limitations.
85+
///
86+
/// ## Errors
87+
///
88+
/// * Create pipe failed.
8589
pub async fn copy<R, W>(r: &mut R, w: &mut W) -> std::io::Result<traffic::TrafficResult>
8690
where
8791
R: io::AsyncReadFd + IsNotFile + Unpin,
@@ -98,6 +102,11 @@ where
98102
///
99103
/// See [`SpliceIoCtx::prepare_reading_file`] for more details; see the
100104
/// [crate-level documentation](crate) for known limitations.
105+
///
106+
/// ## Errors
107+
///
108+
/// * Create pipe failed.
109+
/// * Invalid file length or offset.
101110
pub async fn sendfile<R, W>(
102111
r: &mut R,
103112
w: &mut W,
@@ -126,6 +135,10 @@ where
126135
///
127136
/// See [`SpliceIoCtx::prepare`] and [`SpliceBidiIo::execute`] for more details;
128137
/// see the [crate-level documentation](crate) for known limitations.
138+
///
139+
/// ## Errors
140+
///
141+
/// * Create pipe failed.
129142
pub async fn copy_bidirectional<A, B>(
130143
sl: &mut A,
131144
sr: &mut B,

src/pipe.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub const DEFAULT_PIPE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked
4141

4242
#[derive(Debug)]
4343
/// Linux Pipe.
44-
pub(crate) struct Pipe {
44+
pub struct Pipe {
4545
/// File descriptor for reading from the pipe
4646
read_side_fd: Fd,
4747

@@ -117,7 +117,7 @@ impl Pipe {
117117
/// Create a pipe, with flags `O_NONBLOCK` and `O_CLOEXEC`.
118118
///
119119
/// The default pipe size is set to `MAXIMUM_PIPE_SIZE` bytes.
120-
pub(crate) fn new() -> io::Result<Self> {
120+
pub fn new() -> io::Result<Self> {
121121
pipe_with(PipeFlags::NONBLOCK | PipeFlags::CLOEXEC)
122122
.map_err(|e| io::Error::from_raw_os_error(e.raw_os_error()))
123123
.and_then(|(read_fd, write_fd)| {
@@ -150,7 +150,7 @@ impl Pipe {
150150
/// For more details, see [`fcntl(2)`].
151151
///
152152
/// [`fcntl(2)`]: https://man7.org/linux/man-pages/man2/fcntl.2.html.
153-
pub(crate) fn set_pipe_size(&mut self, pipe_size: usize) -> io::Result<usize> {
153+
pub fn set_pipe_size(&mut self, pipe_size: usize) -> io::Result<usize> {
154154
let Some(write_side_fd) = self.write_side_fd.as_fd() else {
155155
return Err(io::Error::new(
156156
io::ErrorKind::Other,
@@ -173,7 +173,7 @@ impl Pipe {
173173
matches!(self.write_side_fd, Fd::Reserved(_) | Fd::Closed)
174174
}
175175

176-
#[inline(always)]
176+
#[inline]
177177
/// Close the pipe write side file descriptor.
178178
pub(crate) fn set_splice_drain_finished(&mut self) {
179179
self.write_side_fd.set_reserved();
@@ -198,7 +198,7 @@ impl Pipe {
198198

199199
#[inline]
200200
/// Returns the size of the pipe, in bytes.
201-
pub(crate) const fn size(&self) -> NonZeroUsize {
201+
pub const fn size(&self) -> NonZeroUsize {
202202
self.size
203203
}
204204
}

src/pipe/pipe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Pipe pool to reuse pipes.
22
3-
use core::{mem, sync::atomic::AtomicUsize};
3+
use std::{mem, sync::atomic::AtomicUsize};
44
use std::collections::VecDeque;
55
use std::io;
66

0 commit comments

Comments
 (0)