1717//!
1818//! let started = latency.start(&context);
1919//! context.sleep(Duration::from_millis(1)).await;
20- //! started.observe_now(&context);
21- //!
22- //! let value = latency.time(&context, || 7);
23- //! assert_eq!(value, 7);
24- //!
25- //! let missing: Option<u8> = latency.time_some(&context, || None);
26- //! assert!(missing.is_none());
20+ //! started.record(&context);
2721//! });
2822//! ```
2923
@@ -90,9 +84,9 @@ pub struct Timed {
9084
9185/// A sampled histogram observation.
9286///
93- /// This token is explicit: dropping it records nothing. Call [`Started::observe_now `] or
94- /// [`Started::observe_at`] to record a duration.
95- #[ must_use = "call observe_now/observe_at to record the timing, or drop it to skip recording" ]
87+ /// This token is explicit: dropping it records nothing. Call [`Started::record `] to record
88+ /// a duration.
89+ #[ must_use = "call record to record the timing, or drop it to skip recording" ]
9690pub struct Started {
9791 /// The histogram to record durations in.
9892 histogram : Histogram ,
@@ -117,58 +111,16 @@ impl Timed {
117111
118112 /// Sample the current time and return an explicit observation token.
119113 pub fn start < C : Clock + ?Sized > ( & self , clock : & C ) -> Started {
120- self . start_at ( clock. current ( ) )
121- }
122-
123- /// Create an observation token from an existing start time.
124- pub fn start_at ( & self , start : SystemTime ) -> Started {
125114 Started {
126115 histogram : self . histogram . clone ( ) ,
127- start,
116+ start : clock . current ( ) ,
128117 }
129118 }
130-
131- /// Observe the duration between two points in time directly.
132- pub fn observe_between ( & self , start : SystemTime , end : SystemTime ) {
133- self . histogram . observe_between ( start, end) ;
134- }
135-
136- /// Time an operation, always recording the elapsed duration.
137- pub fn time < C : Clock + ?Sized , T , F : FnOnce ( ) -> T > ( & self , clock : & C , f : F ) -> T {
138- let started = self . start ( clock) ;
139- let result = f ( ) ;
140- started. observe_now ( clock) ;
141- result
142- }
143-
144- /// Time an operation, recording only if it returns `Some`.
145- pub fn time_some < C : Clock + ?Sized , T , F : FnOnce ( ) -> Option < T > > (
146- & self ,
147- clock : & C ,
148- f : F ,
149- ) -> Option < T > {
150- let started = self . start ( clock) ;
151- let result = f ( ) ;
152- if result. is_some ( ) {
153- started. observe_now ( clock) ;
154- }
155- result
156- }
157119}
158120
159121impl Started {
160- /// Returns the sampled start time.
161- pub const fn start ( & self ) -> SystemTime {
162- self . start
163- }
164-
165122 /// Record the observation against the current time of `clock`.
166- pub fn observe_now < C : Clock + ?Sized > ( self , clock : & C ) {
167- self . observe_at ( clock. current ( ) ) ;
168- }
169-
170- /// Record the observation against `end`.
171- pub fn observe_at ( self , end : SystemTime ) {
172- self . histogram . observe_between ( self . start , end) ;
123+ pub fn record < C : Clock + ?Sized > ( self , clock : & C ) {
124+ self . histogram . observe_between ( self . start , clock. current ( ) ) ;
173125 }
174126}
0 commit comments