From 05d8551066c7292d6ac43a2e971efd470c9088b2 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Tue, 20 Oct 2020 09:39:53 +0800 Subject: [PATCH] fix misplaced import.relax lifetime bound for actix-rt --- actix-rt/CHANGES.md | 3 ++- actix-rt/src/builder.rs | 8 ++++---- actix-rt/src/system.rs | 2 +- actix-rt/tests/integration_tests.rs | 25 +++++++++++++++++++++++++ actix-server/src/signals.rs | 3 ++- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md index c6cbedd7..232a85d1 100644 --- a/actix-rt/CHANGES.md +++ b/actix-rt/CHANGES.md @@ -8,7 +8,8 @@ ### Changed * update tokio to 0.3 -* Remove `'static` lifetime requirement for `Runtime::block_on`. The method would accept a &Self when calling. +* Remove `'static` lifetime requirement for `Runtime::block_on` and `SystemRunner::block_on`. These methods would accept a &Self when calling. +* Remove `'static` lifetime requirement for `System::run` ## [1.1.1] - 2020-04-30 diff --git a/actix-rt/src/builder.rs b/actix-rt/src/builder.rs index 2760b983..244d1251 100644 --- a/actix-rt/src/builder.rs +++ b/actix-rt/src/builder.rs @@ -65,7 +65,7 @@ impl Builder { /// Function `f` get called within tokio runtime context. pub fn run(self, f: F) -> io::Result<()> where - F: FnOnce() + 'static, + F: FnOnce(), { self.create_runtime(f).run() } @@ -87,7 +87,7 @@ impl Builder { fn create_runtime(self, f: F) -> SystemRunner where - F: FnOnce() + 'static, + F: FnOnce(), { let (stop_tx, stop) = channel(); let (sys_sender, sys_receiver) = unbounded(); @@ -179,9 +179,9 @@ impl SystemRunner { } /// Execute a future and wait for result. - pub fn block_on(&mut self, fut: F) -> O + pub fn block_on(&self, fut: F) -> O where - F: Future + 'static, + F: Future, { Arbiter::run_system(Some(&self.rt)); let res = self.rt.block_on(fut); diff --git a/actix-rt/src/system.rs b/actix-rt/src/system.rs index 651c9d5b..0ead516d 100644 --- a/actix-rt/src/system.rs +++ b/actix-rt/src/system.rs @@ -256,7 +256,7 @@ impl System { /// Function `f` get called within tokio runtime context. pub fn run(f: F) -> io::Result<()> where - F: FnOnce() + 'static, + F: FnOnce(), { Self::builder().run(f) } diff --git a/actix-rt/tests/integration_tests.rs b/actix-rt/tests/integration_tests.rs index c1d2b910..620bb40c 100644 --- a/actix-rt/tests/integration_tests.rs +++ b/actix-rt/tests/integration_tests.rs @@ -112,3 +112,28 @@ fn join_current_arbiter() { "local_join should await only for the already spawned futures" ); } + +#[test] +fn non_static_block_on() { + let string = String::from("test_str"); + let str = string.as_str(); + + let sys = actix_rt::System::new("borrow some"); + + sys.block_on(async { + actix_rt::time::sleep(Duration::from_millis(1)).await; + assert_eq!("test_str", str); + }); + + let rt = actix_rt::Runtime::new().unwrap(); + + rt.block_on(async { + actix_rt::time::sleep(Duration::from_millis(1)).await; + assert_eq!("test_str", str); + }); + + actix_rt::System::run(|| { + assert_eq!("test_str", str); + actix_rt::System::current().stop(); + } ).unwrap(); +} \ No newline at end of file diff --git a/actix-server/src/signals.rs b/actix-server/src/signals.rs index 622b4efb..a677814b 100644 --- a/actix-server/src/signals.rs +++ b/actix-server/src/signals.rs @@ -42,7 +42,6 @@ impl Signals { #[cfg(unix)] { use actix_rt::signal::unix; - use futures_util::stream::Stream; let mut streams = Vec::new(); @@ -86,6 +85,8 @@ impl Future for Signals { } #[cfg(unix)] { + use futures_util::stream::Stream; + for idx in 0..self.streams.len() { loop { match Pin::new(&mut self.streams[idx].1).poll_next(cx) {