From a2e23c023432540fd59b0d530eaea84869ef8c03 Mon Sep 17 00:00:00 2001 From: Jeff Belgum <belgum@google.com> Date: Wed, 8 May 2019 20:34:50 +0000 Subject: [PATCH] [rust] Update Time and Duration methods to the into_* form. It is more idiomatic and avoids ambiguity as to whether e.g. `nanos`, `micros`, and `millis` refers to the subsecond part of a struct or the entire value represented in that unit of measure. Updates all uses of the methods in fuchsia.git and deprecates the old methods. TEST=CQ Change-Id: Ibe67ffd028169cffdac0c1ce662bdb78fe7b1cd4 --- garnet/bin/log_listener/src/main.rs | 3 +- garnet/bin/network-speed-test/src/main.rs | 2 +- garnet/bin/power_manager/src/main.rs | 2 +- garnet/bin/sl4f/src/audio/facade.rs | 4 +- .../examples/mediasession/src/sine_player.rs | 2 +- .../carnelian/examples/spinning_square.rs | 2 +- .../public/rust/fuchsia-zircon/src/channel.rs | 2 +- .../public/rust/fuchsia-zircon/src/handle.rs | 2 +- garnet/public/rust/fuchsia-zircon/src/lib.rs | 4 +- garnet/public/rust/fuchsia-zircon/src/port.rs | 2 +- garnet/public/rust/fuchsia-zircon/src/time.rs | 76 ++++++++++++++----- .../profiles/bt-a2dp-sink/src/main.rs | 2 +- src/connectivity/dhcp/src/main.rs | 2 +- .../wlan/lib/inspect/src/nodes/mod.rs | 4 +- .../wlan/wlanstack/src/telemetry.rs | 8 +- 15 files changed, 77 insertions(+), 40 deletions(-) diff --git a/garnet/bin/log_listener/src/main.rs b/garnet/bin/log_listener/src/main.rs index 6c5d8e885eb..bcbf2a02b39 100644 --- a/garnet/bin/log_listener/src/main.rs +++ b/garnet/bin/log_listener/src/main.rs @@ -227,7 +227,8 @@ impl LocalOptions { // this may offset them from UTC time as set when logged in // case of UTC time adjustments since. let monotonic_zero_as_utc = - zx::Time::get(zx::ClockId::UTC).nanos() - zx::Time::get(zx::ClockId::Monotonic).nanos(); + zx::Time::get(zx::ClockId::UTC).into_nanos() - + zx::Time::get(zx::ClockId::Monotonic).into_nanos(); let shifted_timestamp = monotonic_zero_as_utc + timestamp; let seconds = (shifted_timestamp / 1000000000) as i64; let nanos = (shifted_timestamp % 1000000000) as u32; diff --git a/garnet/bin/network-speed-test/src/main.rs b/garnet/bin/network-speed-test/src/main.rs index ef75704b596..0c68fe22ebe 100644 --- a/garnet/bin/network-speed-test/src/main.rs +++ b/garnet/bin/network-speed-test/src/main.rs @@ -137,7 +137,7 @@ async fn fetch_and_discard_url(http_service: HttpServiceProxy, let bytes_received = await!(socket.copy_into(&mut stdio_sink))?; let stop_time = zx::Time::get(zx::ClockId::Monotonic); - let time_nanos = (stop_time - start_time).nanos() as u64; + let time_nanos = (stop_time - start_time).into_nanos() as u64; let time_seconds = time_nanos as f64 * 1e-9; let bits_received = (bytes_received * 8) as f64; diff --git a/garnet/bin/power_manager/src/main.rs b/garnet/bin/power_manager/src/main.rs index 57af5bb2cee..06ee7b84a70 100644 --- a/garnet/bin/power_manager/src/main.rs +++ b/garnet/bin/power_manager/src/main.rs @@ -35,7 +35,7 @@ struct BatteryStatusHelper { #[inline] fn get_current_time() -> i64 { let t = zx::Time::get(zx::ClockId::UTC); - (t.nanos() / 1000) as i64 + (t.into_nanos() / 1000) as i64 } #[derive(Debug)] diff --git a/garnet/bin/sl4f/src/audio/facade.rs b/garnet/bin/sl4f/src/audio/facade.rs index d5ccb9fa9ea..1719b347c1c 100644 --- a/garnet/bin/sl4f/src/audio/facade.rs +++ b/garnet/bin/sl4f/src/audio/facade.rs @@ -213,7 +213,7 @@ impl OutputWorker { if last_notify > zx::Time::from_nanos(0) { let interval = now - last_notify; if interval > zx::Duration::from_millis(150) { - fx_log_info!("Output position not updated for 150ms({:?}). Expect glitches.", interval.millis()); + fx_log_info!("Output position not updated for 150ms({:?}). Expect glitches.", interval.into_millis()); } } last_notify = now; @@ -545,7 +545,7 @@ impl InputWorker { if last_notify > zx::Time::from_nanos(0) { let interval = now - last_notify; if interval > zx::Duration::from_millis(150) { - fx_log_info!("Input position not updated for 150ms({:?}). Expect glitches.", interval.millis()); + fx_log_info!("Input position not updated for 150ms({:?}). Expect glitches.", interval.into_millis()); } } last_notify = now; diff --git a/garnet/examples/mediasession/src/sine_player.rs b/garnet/examples/mediasession/src/sine_player.rs index d970ad881e2..2686856f9a2 100644 --- a/garnet/examples/mediasession/src/sine_player.rs +++ b/garnet/examples/mediasession/src/sine_player.rs @@ -175,7 +175,7 @@ impl SinePlayer { if state == PlaybackState::Playing { TimelineFunction { subject_time: 0, - reference_time: zx::Time::get(zx::ClockId::Monotonic).nanos(), + reference_time: zx::Time::get(zx::ClockId::Monotonic).into_nanos(), subject_delta: 1, reference_delta: 1, } diff --git a/garnet/public/rust/carnelian/examples/spinning_square.rs b/garnet/public/rust/carnelian/examples/spinning_square.rs index 9b5de1b3e3b..6856b868a3a 100644 --- a/garnet/public/rust/carnelian/examples/spinning_square.rs +++ b/garnet/public/rust/carnelian/examples/spinning_square.rs @@ -119,7 +119,7 @@ impl ViewAssistant for SpinningSquareViewAssistant { )); self.background_node.set_translation(center_x, center_y, BACKGROUND_Z); let square_size = context.size.width.min(context.size.height) * 0.6; - let t = ((context.presentation_time.nanos() - self.start.nanos()) as f32 + let t = ((context.presentation_time.into_nanos() - self.start.into_nanos()) as f32 * SECONDS_PER_NANOSECOND * SPEED) % 1.0; diff --git a/garnet/public/rust/fuchsia-zircon/src/channel.rs b/garnet/public/rust/fuchsia-zircon/src/channel.rs index 4a20ae09d18..a020fcd18b9 100644 --- a/garnet/public/rust/fuchsia-zircon/src/channel.rs +++ b/garnet/public/rust/fuchsia-zircon/src/channel.rs @@ -163,7 +163,7 @@ impl Channel { let status = unsafe { Status::from_raw( sys::zx_channel_call( - self.raw_handle(), options, timeout.nanos(), &args, &mut actual_read_bytes, + self.raw_handle(), options, timeout.into_nanos(), &args, &mut actual_read_bytes, &mut actual_read_handles)) }; unsafe { diff --git a/garnet/public/rust/fuchsia-zircon/src/handle.rs b/garnet/public/rust/fuchsia-zircon/src/handle.rs index 5d7f3afc8a0..1ac39fa9011 100644 --- a/garnet/public/rust/fuchsia-zircon/src/handle.rs +++ b/garnet/public/rust/fuchsia-zircon/src/handle.rs @@ -144,7 +144,7 @@ impl<'a, T: HandleBased> Unowned<'a, T> { let mut pending = Signals::empty().bits(); let status = unsafe { sys::zx_object_wait_one( - self.raw_handle(), signals.bits(), deadline.nanos(), &mut pending) + self.raw_handle(), signals.bits(), deadline.into_nanos(), &mut pending) }; ok(status).map(|()| Signals::from_bits_truncate(pending)) } diff --git a/garnet/public/rust/fuchsia-zircon/src/lib.rs b/garnet/public/rust/fuchsia-zircon/src/lib.rs index 7f45e34e3e1..a78af60b1fb 100644 --- a/garnet/public/rust/fuchsia-zircon/src/lib.rs +++ b/garnet/public/rust/fuchsia-zircon/src/lib.rs @@ -247,7 +247,7 @@ pub enum ClockId { pub fn object_wait_many(items: &mut [WaitItem], deadline: Time) -> Result<bool, Status> { let items_ptr = items.as_mut_ptr() as *mut sys::zx_wait_item_t; - let status = unsafe { sys::zx_object_wait_many( items_ptr, items.len(), deadline.nanos()) }; + let status = unsafe { sys::zx_object_wait_many( items_ptr, items.len(), deadline.into_nanos()) }; if status == sys::ZX_ERR_CANCELED { return Ok(true) } @@ -348,7 +348,7 @@ mod tests { let ticks2 = ticks_get(); // The number of ticks should have increased by at least 1 ms worth - let sleep_ticks = (sleep_time.millis() as u64) * ticks_per_second() / 1000; + let sleep_ticks = (sleep_time.into_millis() as u64) * ticks_per_second() / 1000; assert!(ticks2 >= (ticks1 + sleep_ticks)); } diff --git a/garnet/public/rust/fuchsia-zircon/src/port.rs b/garnet/public/rust/fuchsia-zircon/src/port.rs index 1d8ae6384ac..61787e3b7db 100644 --- a/garnet/public/rust/fuchsia-zircon/src/port.rs +++ b/garnet/public/rust/fuchsia-zircon/src/port.rs @@ -323,7 +323,7 @@ impl Port { pub fn wait(&self, deadline: Time) -> Result<Packet, Status> { let mut packet = Default::default(); let status = unsafe { - sys::zx_port_wait(self.raw_handle(), deadline.nanos(), + sys::zx_port_wait(self.raw_handle(), deadline.into_nanos(), &mut packet as *mut sys::zx_port_packet_t) }; ok(status)?; diff --git a/garnet/public/rust/fuchsia-zircon/src/time.rs b/garnet/public/rust/fuchsia-zircon/src/time.rs index 01187a57ea8..812a0775f8f 100644 --- a/garnet/public/rust/fuchsia-zircon/src/time.rs +++ b/garnet/public/rust/fuchsia-zircon/src/time.rs @@ -28,66 +28,66 @@ impl From<stdtime::Duration> for Duration { impl ops::Add<Duration> for Time { type Output = Time; fn add(self, dur: Duration) -> Time { - Time::from_nanos(dur.nanos() + self.nanos()) + Time::from_nanos(dur.into_nanos() + self.into_nanos()) } } impl ops::Add<Time> for Duration { type Output = Time; fn add(self, time: Time) -> Time { - Time::from_nanos(self.nanos() + time.nanos()) + Time::from_nanos(self.into_nanos() + time.into_nanos()) } } impl ops::Add for Duration { type Output = Duration; fn add(self, dur: Duration) -> Duration { - Duration::from_nanos(self.nanos() + dur.nanos()) + Duration::from_nanos(self.into_nanos() + dur.into_nanos()) } } impl ops::Sub for Duration { type Output = Duration; fn sub(self, dur: Duration) -> Duration { - Duration::from_nanos(self.nanos() - dur.nanos()) + Duration::from_nanos(self.into_nanos() - dur.into_nanos()) } } impl ops::Sub<Duration> for Time { type Output = Time; fn sub(self, dur: Duration) -> Time { - Time::from_nanos(self.nanos() - dur.nanos()) + Time::from_nanos(self.into_nanos() - dur.into_nanos()) } } impl ops::Sub<Time> for Time { type Output = Duration; fn sub(self, other: Time) -> Duration { - Duration::from_nanos(self.nanos() - other.nanos()) + Duration::from_nanos(self.into_nanos() - other.into_nanos()) } } impl ops::AddAssign for Duration { fn add_assign(&mut self, dur: Duration) { - self.0 += dur.nanos() + self.0 += dur.into_nanos() } } impl ops::SubAssign for Duration { fn sub_assign(&mut self, dur: Duration) { - self.0 -= dur.nanos() + self.0 -= dur.into_nanos() } } impl ops::AddAssign<Duration> for Time { fn add_assign(&mut self, dur: Duration) { - self.0 += dur.nanos() + self.0 += dur.into_nanos() } } impl ops::SubAssign<Duration> for Time { fn sub_assign(&mut self, dur: Duration) { - self.0 -= dur.nanos() + self.0 -= dur.into_nanos() } } @@ -115,28 +115,64 @@ impl Duration { Time::after(self).sleep() } + #[deprecated(note = "Users should instead use into_nanos")] pub fn nanos(self) -> i64 { self.0 } + #[deprecated(note = "Users should instead use into_micros")] pub fn micros(self) -> i64 { self.0 / 1_000 } + #[deprecated(note = "Users should instead use into_millis")] pub fn millis(self) -> i64 { - self.micros() / 1_000 + self.into_micros() / 1_000 } + #[deprecated(note = "Users should instead use into_seconds")] pub fn seconds(self) -> i64 { - self.millis() / 1_000 + self.into_millis() / 1_000 } + #[deprecated(note = "Users should instead use into_minutes")] pub fn minutes(self) -> i64 { - self.seconds() / 60 + self.into_seconds() / 60 } + #[deprecated(note = "Users should instead use into_hours")] pub fn hours(self) -> i64 { - self.minutes() / 60 + self.into_minutes() / 60 + } + + /// Returns the number of nanoseconds contained by this `Duration`. + pub fn into_nanos(self) -> i64 { + self.0 + } + + /// Returns the total number of whole microseconds contained by this `Duration`. + pub fn into_micros(self) -> i64 { + self.0 / 1_000 + } + + /// Returns the total number of whole milliseconds contained by this `Duration`. + pub fn into_millis(self) -> i64 { + self.into_micros() / 1_000 + } + + /// Returns the total number of whole seconds contained by this `Duration`. + pub fn into_seconds(self) -> i64 { + self.into_millis() / 1_000 + } + + /// Returns the total number of whole minutes contained by this `Duration`. + pub fn into_minutes(self) -> i64 { + self.into_seconds() / 60 + } + + /// Returns the total number of whole hours contained by this `Duration`. + pub fn into_hours(self) -> i64 { + self.into_minutes() / 60 } pub fn from_nanos(nanos: i64) -> Self { @@ -245,7 +281,7 @@ impl Time { unsafe { sys::zx_nanosleep(self.0); } } - pub fn nanos(self) -> i64 { + pub fn into_nanos(self) -> i64 { self.0 } @@ -303,7 +339,7 @@ impl Timer { /// syscall. pub fn set(&self, deadline: Time, slack: Duration) -> Result<(), Status> { let status = unsafe { - sys::zx_timer_set(self.raw_handle(), deadline.nanos(), slack.nanos()) + sys::zx_timer_set(self.raw_handle(), deadline.into_nanos(), slack.into_nanos()) }; ok(status) } @@ -333,7 +369,7 @@ mod tests { let std_dur = stdtime::Duration::new(25, 25); let dur = Duration::from(std_dur); let std_dur_nanos = (1_000_000_000 * std_dur.as_secs()) + std_dur.subsec_nanos() as u64; - assert_eq!(std_dur_nanos as i64, dur.nanos()); + assert_eq!(std_dur_nanos as i64, dur.into_nanos()); } #[test] @@ -342,9 +378,9 @@ mod tests { let dur_from_nanos = Duration::from_nanos(nanos_in_one_hour); let dur_from_hours = Duration::from_hours(1); assert_eq!(dur_from_nanos, dur_from_hours); - assert_eq!(dur_from_nanos.nanos(), dur_from_hours.nanos()); - assert_eq!(dur_from_nanos.nanos(), nanos_in_one_hour); - assert_eq!(dur_from_nanos.hours(), 1); + assert_eq!(dur_from_nanos.into_nanos(), dur_from_hours.into_nanos()); + assert_eq!(dur_from_nanos.into_nanos(), nanos_in_one_hour); + assert_eq!(dur_from_nanos.into_hours(), 1); } #[test] diff --git a/src/connectivity/bluetooth/profiles/bt-a2dp-sink/src/main.rs b/src/connectivity/bluetooth/profiles/bt-a2dp-sink/src/main.rs index 5146b451c14..e4f932eba5e 100644 --- a/src/connectivity/bluetooth/profiles/bt-a2dp-sink/src/main.rs +++ b/src/connectivity/bluetooth/profiles/bt-a2dp-sink/src/main.rs @@ -512,7 +512,7 @@ async fn decode_media_stream( metrics::A2DP_NUMBER_OF_MICROSECONDS_STREAMED_METRIC_ID, metrics::A2dpNumberOfMicrosecondsStreamedMetricDimensionCodec::Sbc as u32, 0, - (end_time - start_time).micros(), + (end_time - start_time).into_micros(), ); } diff --git a/src/connectivity/dhcp/src/main.rs b/src/connectivity/dhcp/src/main.rs index b37a90966dc..453f389f8a2 100644 --- a/src/connectivity/dhcp/src/main.rs +++ b/src/connectivity/dhcp/src/main.rs @@ -45,7 +45,7 @@ fn main() -> Result<(), Error> { let udp_socket = UdpSocket::bind(&socket_addr).context("unable to bind socket")?; udp_socket.set_broadcast(true).context("unable to set broadcast")?; let server = Mutex::new(Server::from_config(config, || { - zx::Time::get(zx::ClockId::UTC).nanos() / 1_000_000_000 + zx::Time::get(zx::ClockId::UTC).into_nanos() / 1_000_000_000 })); let msg_handling_loop = define_msg_handling_loop_future(udp_socket, &server); let lease_expiration_handler = define_lease_expiration_handler_future(&server); diff --git a/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs b/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs index fd9be02c1c1..335df2702d2 100644 --- a/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs +++ b/src/connectivity/wlan/lib/inspect/src/nodes/mod.rs @@ -45,8 +45,8 @@ impl NodeExt for finspect::ObjectTreeNode { fn set_time_at(&mut self, timestamp: zx::Time) -> &mut Self { // TODO(WLAN-1010) - if we have something to post-process Inspect JSON dump, it would be // better to log the timestamp as MetricValue::UintValue. - let seconds = timestamp.nanos() / 1000_000_000; - let millis = (timestamp.nanos() % 1000_000_000) / 1000_000; + let seconds = timestamp.into_nanos() / 1000_000_000; + let millis = (timestamp.into_nanos() % 1000_000_000) / 1000_000; self.add_property(fidl_inspect::Property { key: "@time".to_string(), value: fidl_inspect::PropertyValue::Str(format!("{}.{}", seconds, millis)), diff --git a/src/connectivity/wlan/wlanstack/src/telemetry.rs b/src/connectivity/wlan/wlanstack/src/telemetry.rs index a611e0e74ef..a5bf0783260 100644 --- a/src/connectivity/wlan/wlanstack/src/telemetry.rs +++ b/src/connectivity/wlan/wlanstack/src/telemetry.rs @@ -231,7 +231,7 @@ pub fn report_scan_delay( scan_started_time: zx::Time, scan_finished_time: zx::Time, ) { - let delay_micros = (scan_finished_time - scan_started_time).micros(); + let delay_micros = (scan_finished_time - scan_started_time).into_micros(); sender.log_elapsed_time(metrics::SCAN_DELAY_METRIC_ID, (), delay_micros); } @@ -244,7 +244,7 @@ pub fn report_connection_delay( ) { use wlan_metrics_registry::ConnectionDelayMetricDimensionConnectionResult::{Fail, Success}; - let delay_micros = (conn_finished_time - conn_started_time).micros(); + let delay_micros = (conn_finished_time - conn_started_time).into_micros(); let connection_result_cobalt = match (result, failure) { (ConnectResult::Success, None) => Some(Success), (ConnectResult::Success, Some(_)) => None, @@ -266,7 +266,7 @@ pub fn report_assoc_success_delay( assoc_started_time: zx::Time, assoc_finished_time: zx::Time, ) { - let delay_micros = (assoc_finished_time - assoc_started_time).micros(); + let delay_micros = (assoc_finished_time - assoc_started_time).into_micros(); sender.log_elapsed_time(metrics::ASSOCIATION_DELAY_METRIC_ID, 0, delay_micros); } @@ -275,7 +275,7 @@ pub fn report_rsna_established_delay( rsna_started_time: zx::Time, rsna_finished_time: zx::Time, ) { - let delay_micros = (rsna_finished_time - rsna_started_time).micros(); + let delay_micros = (rsna_finished_time - rsna_started_time).into_micros(); sender.log_elapsed_time(metrics::RSNA_DELAY_METRIC_ID, 0, delay_micros); } -- GitLab