• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use std::future::Future;
16 use std::pin::Pin;
17 use std::task::{Context, Poll};
18 use std::time::Duration;
19 
20 use tokio::time::{sleep, Sleep};
21 
22 /// Pinned Sleep instance. It can be used in tokio::select! macro.
23 pub(super) struct PinSleep(Pin<Box<Sleep>>);
24 
25 impl PinSleep {
new(duration: Duration) -> Self26     pub fn new(duration: Duration) -> Self {
27         Self(Box::pin(sleep(duration)))
28     }
29 }
30 
31 impl Future for PinSleep {
32     type Output = ();
33 
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>34     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
35         self.0.as_mut().poll(cx)
36     }
37 }
38 
39 /// Generate the setter method for the field of the struct for the builder pattern.
40 macro_rules! builder_field {
41     ($field:ident, $ty:ty, $wrap:expr) => {
42         pub fn $field(&mut self, value: $ty) -> &mut Self {
43             self.$field = $wrap(value);
44             self
45         }
46     };
47     ($field:ident, $ty:ty) => {
48         builder_field!($field, $ty, ::std::convert::identity);
49     };
50 }
51 pub(crate) use builder_field;
52 
53 #[cfg(test)]
init_test_logging()54 pub fn init_test_logging() {
55     let _ = env_logger::builder().is_test(true).try_init();
56 }
57