Skip to content

Commit

Permalink
move track caller into func
Browse files Browse the repository at this point in the history
Signed-off-by: Zahari Dichev <zaharidichev@gmail.com>
  • Loading branch information
zaharidichev committed Sep 14, 2021
1 parent 148a989 commit ad39bb6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
16 changes: 4 additions & 12 deletions tokio/src/time/driver/sleep.rs
@@ -1,5 +1,6 @@
use crate::time::driver::{Handle, TimerEntry};
use crate::time::{error::Error, Duration, Instant};
use crate::util::trace;

use pin_project_lite::pin_project;
use std::future::Future;
Expand Down Expand Up @@ -46,13 +47,7 @@ cfg_trace! {
#[cfg_attr(docsrs, doc(alias = "delay_until"))]
#[cfg_attr(tokio_track_caller, track_caller)]
pub fn sleep_until(deadline: Instant) -> Sleep {
#[cfg(tokio_track_caller)]
let location = std::panic::Location::caller();
#[cfg(tokio_track_caller)]
return Sleep::new_timeout(deadline, Some(location));

#[cfg(not(tokio_track_caller))]
Sleep::new_timeout(deadline, None)
return Sleep::new_timeout(deadline, trace::caller_location());
}

/// Waits until `duration` has elapsed.
Expand Down Expand Up @@ -96,10 +91,7 @@ pub fn sleep_until(deadline: Instant) -> Sleep {
#[cfg_attr(docsrs, doc(alias = "wait"))]
#[cfg_attr(tokio_track_caller, track_caller)]
pub fn sleep(duration: Duration) -> Sleep {
#[cfg(tokio_track_caller)]
let location = Some(std::panic::Location::caller());
#[cfg(not(tokio_track_caller))]
let location = None;
let location = trace::caller_location();

match Instant::now().checked_add(duration) {
Some(deadline) => Sleep::new_timeout(deadline, location),
Expand Down Expand Up @@ -226,7 +218,7 @@ cfg_not_trace! {
}

impl Sleep {
#[allow(unused_variables)]
#[cfg_attr(not(all(tokio_unstable, feature = "tracing")), allow(unused_variables))]
pub(crate) fn new_timeout(
deadline: Instant,
location: Option<&'static Location<'static>>,
Expand Down
13 changes: 5 additions & 8 deletions tokio/src/time/timeout.rs
Expand Up @@ -4,16 +4,16 @@
//!
//! [`Timeout`]: struct@Timeout

use crate::time::{error::Elapsed, sleep_until, Duration, Instant, Sleep};
use crate::{
time::{error::Elapsed, sleep_until, Duration, Instant, Sleep},
util::trace,
};

use pin_project_lite::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::task::{self, Poll};

#[cfg(all(tokio_track_caller, tokio_unstable, feature = "tracing"))]
use std::panic::Location;

/// Require a `Future` to complete before the specified duration has elapsed.
///
/// If the future completes before the duration has elapsed, then the completed
Expand Down Expand Up @@ -53,10 +53,7 @@ pub fn timeout<T>(duration: Duration, future: T) -> Timeout<T>
where
T: Future,
{
#[cfg(all(tokio_track_caller, tokio_unstable, feature = "tracing"))]
let location = Some(Location::caller());
#[cfg(not(all(tokio_track_caller, tokio_unstable, feature = "tracing")))]
let location = None;
let location = trace::caller_location();

let deadline = Instant::now().checked_add(duration);
let delay = match deadline {
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/util/trace.rs
Expand Up @@ -27,6 +27,15 @@ cfg_trace! {
}
}
}
cfg_time! {
#[cfg_attr(tokio_track_caller, track_caller)]
pub(crate) fn caller_location() -> Option<&'static std::panic::Location<'static>> {
#[cfg(all(tokio_track_caller, tokio_unstable, feature = "tracing"))]
return Some(std::panic::Location::caller());
#[cfg(not(all(tokio_track_caller, tokio_unstable, feature = "tracing")))]
None
}
}

cfg_not_trace! {
cfg_rt! {
Expand Down

0 comments on commit ad39bb6

Please sign in to comment.