Skip to content

Commit 69a3867

Browse files
committed
chore: Optimize code 2023
1 parent 366bcc4 commit 69a3867

17 files changed

+49
-50
lines changed

benches/body.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(test)]
2-
#![deny(warnings)]
2+
#![allow(deprecated)]
33

44
extern crate test;
55

@@ -13,7 +13,7 @@ use test::Bencher;
1313

1414
#[bench]
1515
fn bench_task_spwan(b: &mut Bencher) {
16-
let body = move |_| create_default_delay_task_handler();
16+
let body = move || async {};
1717

1818
let mut task_builder = TaskBuilder::default();
1919
task_builder
@@ -23,16 +23,16 @@ fn bench_task_spwan(b: &mut Bencher) {
2323

2424
// String parsing to corn-expression -> iterator is the most time-consuming operation about 1500ns ~ 3500 ns.
2525
// The iterator is used to find out when the next execution should take place, in about 500 ns.
26-
b.iter(|| task_builder.spawn_async_routine(body.clone()));
26+
b.iter(|| task_builder.spawn_async_routine(body));
2727
}
2828

2929
#[bench]
3030
fn bench_maintain_task(b: &mut Bencher) {
3131
let (timer_event_sender, _timer_event_receiver) = unbounded::<TimerEvent>();
3232
let shared_header = SharedHeader::default();
33-
let mut timer = Timer::new(timer_event_sender.clone(), shared_header);
33+
let mut timer = Timer::new(timer_event_sender, shared_header);
3434

35-
let body = move |_| create_default_delay_task_handler();
35+
let body = move || async {};
3636

3737
let mut task_builder = TaskBuilder::default();
3838
task_builder
@@ -57,12 +57,12 @@ fn bench_maintain_task(b: &mut Bencher) {
5757
fn bench_try_wait(b: &mut Bencher) {
5858
use std::process::Command;
5959

60-
if let Ok(mut child) = Command::new("ps").spawn_async_routine() {
60+
if let Ok(mut child) = Command::new("ps").spawn() {
6161
b.iter(|| child.try_wait());
6262
}
6363
}
6464

6565
#[bench]
6666
fn bench_timestamp(b: &mut Bencher) {
67-
b.iter(|| timestamp());
67+
b.iter(timestamp);
6868
}

examples/cycle_tokio_task.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,25 @@ pub async fn async_template(id: i32, name: String) -> Result<()> {
6767
// The default connector does not handle TLS.
6868
// Speaking to https destinations will require configuring a connector that implements TLS.
6969
// So use http for test.
70-
let url = format!("http://httpbin.org/get?id={}&name={}", id, name);
70+
let url = format!("http://httpbin.org/get?id={id}&name={name}");
7171
let uri: Uri = url.parse()?;
7272

7373
let res = client.get(uri).await?;
7474
println!("Response: {}", res.status());
7575
// Concatenate the body stream into a single buffer...
7676
let buf = hyper::body::to_bytes(res).await?;
77-
println!("body: {:?}", buf);
77+
println!("body: {buf:?}");
7878
Ok(())
7979
}
8080

8181
enum AuspiciousDay {
8282
Wake,
8383
}
8484

85-
impl Into<CandyCronStr> for AuspiciousDay {
86-
fn into(self) -> CandyCronStr {
87-
match self {
88-
Self::Wake => CandyCronStr("0 * * * Jan-Dec * 2020-2100".to_string()),
85+
impl From<AuspiciousDay> for CandyCronStr {
86+
fn from(val: AuspiciousDay) -> Self {
87+
match val {
88+
AuspiciousDay::Wake => CandyCronStr("0 * * * Jan-Dec * 2020-2100".to_string()),
8989
}
9090
}
9191
}

examples/demo.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use delay_timer::utils::convenience::functions::unblock_process_task_fn;
55
use smol::Timer;
66
use std::thread::{current, park, Thread};
77
use std::time::Duration;
8-
use surf;
98

109
// cargo run --package delay_timer --example demo --features=full
1110

@@ -115,7 +114,7 @@ fn build_task_customized_async_task() -> Result<Task, TaskError> {
115114
}
116115

117116
pub async fn async_template(id: i32, name: String) {
118-
let url = format!("https://httpbin.org/get?id={}&name={}", id, name);
117+
let url = format!("https://httpbin.org/get?id={id}&name={name}");
119118
if let Ok(mut res) = surf::get(url).await {
120119
dbg!(res.body_string().await.unwrap_or_default());
121120
}
@@ -146,13 +145,15 @@ enum AuspiciousTime {
146145
PerDayFiveAclock,
147146
}
148147

149-
impl Into<CandyCronStr> for AuspiciousTime {
150-
fn into(self) -> CandyCronStr {
151-
match self {
152-
Self::PerSevenSeconds => CandyCronStr("0/7 * * * * * *".to_string()),
153-
Self::PerEightSeconds => CandyCronStr("0/8 * * * * * *".to_string()),
154-
Self::LoveTime => CandyCronStr("0,10,15,25,50 0/1 * * Jan-Dec * 2020-2100".to_string()),
155-
Self::PerDayFiveAclock => CandyCronStr("01 00 1 * * * *".to_string()),
148+
impl From<AuspiciousTime> for CandyCronStr {
149+
fn from(val: AuspiciousTime) -> Self {
150+
match val {
151+
AuspiciousTime::PerSevenSeconds => CandyCronStr("0/7 * * * * * *".to_string()),
152+
AuspiciousTime::PerEightSeconds => CandyCronStr("0/8 * * * * * *".to_string()),
153+
AuspiciousTime::LoveTime => {
154+
CandyCronStr("0,10,15,25,50 0/1 * * Jan-Dec * 2020-2100".to_string())
155+
}
156+
AuspiciousTime::PerDayFiveAclock => CandyCronStr("01 00 1 * * * *".to_string()),
156157
}
157158
}
158159
}

examples/demo_async_tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ pub async fn async_template(id: i32, name: String) -> Result<()> {
100100
// The default connector does not handle TLS.
101101
// Speaking to https destinations will require configuring a connector that implements TLS.
102102
// So use http for test.
103-
let url = format!("http://httpbin.org/get?id={}&name={}", id, name);
103+
let url = format!("http://httpbin.org/get?id={id}&name={name}");
104104
let uri: Uri = url.parse()?;
105105

106106
let res = client.get(uri).await?;
107107
println!("Response: {}", res.status());
108108
// Concatenate the body stream into a single buffer...
109109
let buf = hyper::body::to_bytes(res).await?;
110-
println!("body: {:?}", buf);
110+
println!("body: {buf:?}");
111111
Ok(())
112112
}

examples/dynamic_cancel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() -> Result<()> {
1414
// Dynamically cancel in the context of `synchronization`.
1515
sync_cancel()?;
1616

17-
println!("");
17+
println!();
1818
// Dynamic cancellation in `asynchronous` contexts.
1919
async_cancel()
2020
}

examples/generic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use smol::Timer;
66
use std::any::{type_name, Any};
77
use std::thread::park_timeout;
88
use std::time::Duration;
9-
use surf;
109

1110
// cargo run --package delay_timer --example generic --features=full
1211

@@ -33,7 +32,7 @@ fn build_generic_task_async_request<T: Animal>(animal: T) -> Result<Task, TaskEr
3332

3433
let body = move || {
3534
let animal_ref = animal.clone();
36-
let other_animal_ref = other_animal.clone();
35+
let other_animal_ref = other_animal;
3736
async move {
3837
if let Ok(mut res) = surf::get("https://httpbin.org/get").await {
3938
dbg!(res.body_string().await.unwrap_or_default());

examples/increase.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![allow(deprecated)]
22

3-
use surf;
4-
53
use delay_timer::prelude::*;
64
use std::ops::Deref;
75
use std::ptr::NonNull;
@@ -84,7 +82,7 @@ fn get_increase_fn(run_flag_ref: SafePointer) -> impl Copy + Fn() {
8482
fn get_wake_fn(
8583
thread: Thread,
8684
run_flag_ref: SafePointer,
87-
) -> impl Fn() -> () + Clone + Send + Sync + 'static {
85+
) -> impl Fn() + Clone + Send + Sync + 'static {
8886
move || {
8987
let local_run_flag = run_flag_ref.as_ptr();
9088
unsafe {
@@ -102,7 +100,7 @@ fn get_async_fn() -> impl std::future::Future {
102100
async {
103101
if let Ok(mut res) = surf::get("https://httpbin.org/get").await {
104102
let body_str = res.body_string().await.unwrap_or_default();
105-
println!("{}", body_str);
103+
println!("{body_str}");
106104
}
107105
}
108106
}

examples/profile_memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ impl RequstBody {
2121
}
2222
}
2323

24-
impl Into<CandyCronStr> for RequstBody {
25-
fn into(self) -> CandyCronStr {
26-
CandyCronStr(self.cron_expression)
24+
impl From<RequstBody> for CandyCronStr {
25+
fn from(val: RequstBody) -> Self {
26+
CandyCronStr(val.cron_expression)
2727
}
2828
}
2929

3030
// LD_PRELOAD=../../tools-bin/libmemory_profiler.so ./target/debug/examples/profile_memory
3131
// ../../tools-bin/memory-profiler-cli server memory-profiling_*.dat
3232
fn main() {
33-
let capacity: usize = 256_00;
33+
let capacity: usize = 25_600;
3434
let mut task_builder_vec: Vec<TaskBuilder> = Vec::with_capacity(capacity);
3535

3636
for _ in 0..capacity {

examples/share_tokio_runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() -> Result<(), Report> {
2727
}
2828
})?;
2929

30-
Ok(read_config()?)
30+
read_config()
3131
}
3232

3333
fn build_task_async_print(id: u64, cron_str: &'static str) -> Result<Task, TaskError> {

src/entity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl RuntimeInstance {
179179
.thread_name_fn(|| {
180180
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
181181
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
182-
format!("tokio-{}", id)
182+
format!("tokio-{id}")
183183
})
184184
.on_thread_start(|| {
185185
debug!("tokio-thread started");

0 commit comments

Comments
 (0)