Skip to content

Commit

Permalink
Rename Duration/Instant to FuriDuration/FuriInstant
Browse files Browse the repository at this point in the history
This is to prevent confusion between the two similar but not
interchangeable types.
  • Loading branch information
dcoles committed Jan 31, 2025
1 parent 4f060e1 commit 3b29ef6
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 182 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Changed

- `flipperzero::furi::time::Duration` has been renamed to `FuriDuration`
- `flipperzero::furi::time::Instant` has been renamed to `FuriInstant`

### Removed

## [0.14.0]
Expand Down
4 changes: 2 additions & 2 deletions crates/flipperzero/examples/i2c-ds3231.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate flipperzero_alloc;

use core::ffi::CStr;

use flipperzero::{error, furi::time::Duration, gpio::i2c, println};
use flipperzero::{error, furi::time::FuriDuration, gpio::i2c, println};
use flipperzero_rt::{entry, manifest};
use ufmt::derive::uDebug;

Expand Down Expand Up @@ -66,7 +66,7 @@ impl RtcTime {
fn main(_args: Option<&CStr>) -> i32 {
let mut bus = i2c::Bus::EXTERNAL.acquire();
let rtc = i2c::DeviceAddress::new(0x68);
let timeout = Duration::from_millis(50);
let timeout = FuriDuration::from_millis(50);

if bus.is_device_ready(rtc, timeout) {
let mut data = [0; 7];
Expand Down
2 changes: 1 addition & 1 deletion crates/flipperzero/examples/stream_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use flipperzero::{furi, println};
use flipperzero_rt::{entry, manifest};

use core::time::Duration as CoreDuration;
use furi::time::Duration as FuriDuration;
use furi::time::FuriDuration;

// Define the FAP Manifest for this application
manifest!(name = "Stream buffer example");
Expand Down
18 changes: 9 additions & 9 deletions crates/flipperzero/src/furi/message_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use flipperzero_sys as sys;
use flipperzero_sys::furi::Status;

use crate::furi;
use crate::furi::time::Duration;
use crate::furi::time::FuriDuration;

/// MessageQueue provides a safe wrapper around the furi message queue primitive.
pub struct MessageQueue<M: Sized> {
Expand All @@ -28,7 +28,7 @@ impl<M: Sized> MessageQueue<M> {
}

// Attempts to add the message to the end of the queue, waiting up to timeout ticks.
pub fn put(&self, msg: M, timeout: Duration) -> furi::Result<()> {
pub fn put(&self, msg: M, timeout: FuriDuration) -> furi::Result<()> {
let mut msg = core::mem::ManuallyDrop::new(msg);

let status: Status = unsafe {
Expand All @@ -46,7 +46,7 @@ impl<M: Sized> MessageQueue<M> {
}

// Attempts to read a message from the front of the queue within timeout ticks.
pub fn get(&self, timeout: Duration) -> furi::Result<M> {
pub fn get(&self, timeout: FuriDuration) -> furi::Result<M> {
let mut out = core::mem::MaybeUninit::<M>::uninit();
let status: Status = unsafe {
sys::furi_message_queue_get(
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<M: Sized> Drop for MessageQueue<M> {
// Drain any elements from the message queue, so any
// drop handlers on the message element get called.
while !self.is_empty() {
match self.get(Duration::MAX) {
match self.get(FuriDuration::MAX) {
Ok(msg) => drop(msg),
Err(_) => break, // we tried
}
Expand All @@ -112,26 +112,26 @@ mod tests {
assert_eq!(queue.capacity(), 3);

// Adding a message to the queue should consume capacity.
queue.put(2, Duration::from_millis(1)).unwrap();
queue.put(2, FuriDuration::from_millis(1)).unwrap();
assert_eq!(queue.len(), 1);
assert_eq!(queue.space(), 2);
assert_eq!(queue.capacity(), 3);

// We should be able to fill the queue to capacity.
queue.put(4, Duration::from_millis(1)).unwrap();
queue.put(6, Duration::from_millis(1)).unwrap();
queue.put(4, FuriDuration::from_millis(1)).unwrap();
queue.put(6, FuriDuration::from_millis(1)).unwrap();
assert_eq!(queue.len(), 3);
assert_eq!(queue.space(), 0);
assert_eq!(queue.capacity(), 3);

// Attempting to add another message should time out.
assert_eq!(
queue.put(7, Duration::from_millis(1)),
queue.put(7, FuriDuration::from_millis(1)),
Err(furi::Error::TimedOut),
);

// Removing a message from the queue frees up capacity.
assert_eq!(queue.get(Duration::from_millis(1)), Ok(2));
assert_eq!(queue.get(FuriDuration::from_millis(1)), Ok(2));
assert_eq!(queue.len(), 2);
assert_eq!(queue.space(), 1);
assert_eq!(queue.capacity(), 3);
Expand Down
31 changes: 19 additions & 12 deletions crates/flipperzero/src/furi/stream_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl StreamBuffer {
/// The function copies the bytes into the buffer, returning the number of bytes successfully
/// sent.
/// It blocks if not enough space is available until the data is sent or the timeout expires.
/// Passing [`Duration::ZERO`](furi::time::Duration::ZERO) immediately returns with as many
/// bytes as can fit, while [`Duration::WAIT_FOREVER`](furi::time::Duration::WAIT_FOREVER) waits
/// Passing [`FuriDuration::ZERO`](furi::time::FuriDuration::ZERO) immediately returns with as many
/// bytes as can fit, while [`FuriDuration::WAIT_FOREVER`](furi::time::FuriDuration::WAIT_FOREVER) waits
/// indefinitely.
///
/// # Safety
Expand All @@ -122,7 +122,7 @@ impl StreamBuffer {
/// # Interrupt Routines
///
/// The `timeout` is ignored when called from an interrupt routine.
pub unsafe fn send(&self, data: &[u8], timeout: furi::time::Duration) -> usize {
pub unsafe fn send(&self, data: &[u8], timeout: furi::time::FuriDuration) -> usize {
let self_ptr = self.0.as_ptr();
let data_ptr = data.as_ptr().cast();
let data_len = data.len();
Expand All @@ -136,8 +136,8 @@ impl StreamBuffer {
/// received.
/// The function blocks until the [trigger level](Self::set_trigger_level) is reached, the
/// buffer is filled, or the timeout expires.
/// Passing [`Duration::ZERO`](furi::time::Duration::ZERO) returns immediately with as many
/// bytes as available, while [`Duration::WAIT_FOREVER`](furi::time::Duration::WAIT_FOREVER)
/// Passing [`FuriDuration::ZERO`](furi::time::FuriDuration::ZERO) returns immediately with as many
/// bytes as available, while [`FuriDuration::WAIT_FOREVER`](furi::time::FuriDuration::WAIT_FOREVER)
/// waits indefinitely.
///
/// # Safety
Expand All @@ -154,7 +154,7 @@ impl StreamBuffer {
/// # Interrupt Routines
///
/// The `timeout` is ignored when called from an interrupt routine.
pub unsafe fn receive(&self, data: &mut [u8], timeout: furi::time::Duration) -> usize {
pub unsafe fn receive(&self, data: &mut [u8], timeout: furi::time::FuriDuration) -> usize {
let self_ptr = self.0.as_ptr();
let data_ptr: *mut c_void = data.as_mut_ptr().cast();
let data_len = data.len();
Expand Down Expand Up @@ -324,7 +324,7 @@ mod stream {
/// If the underlying stream buffer does not have enough free space, it sends only the bytes
/// that fit and returns immediately.
pub fn send(&self, data: &[u8]) -> usize {
unsafe { self.buffer_ref.send(data, furi::time::Duration::ZERO) }
unsafe { self.buffer_ref.send(data, furi::time::FuriDuration::ZERO) }
}

/// Sends bytes in a blocking manner.
Expand All @@ -337,7 +337,7 @@ mod stream {
pub fn send_blocking(&self, data: &[u8]) -> usize {
unsafe {
self.buffer_ref
.send(data, furi::time::Duration::WAIT_FOREVER)
.send(data, furi::time::FuriDuration::WAIT_FOREVER)
}
}

Expand All @@ -350,7 +350,7 @@ mod stream {
/// # Interrupt Routines
///
/// In an interrupt routine, this method behaves like [`send`](Self::send).
pub fn send_with_timeout(&self, data: &[u8], timeout: furi::time::Duration) -> usize {
pub fn send_with_timeout(&self, data: &[u8], timeout: furi::time::FuriDuration) -> usize {
unsafe { self.buffer_ref.send(data, timeout) }
}
}
Expand Down Expand Up @@ -434,7 +434,10 @@ mod stream {
/// It will either receive all available bytes or fill the buffer, whichever happens first.
/// Returns the number of bytes successfully received.
pub fn recv(&self, data: &mut [u8]) -> usize {
unsafe { self.buffer_ref.receive(data, furi::time::Duration::ZERO) }
unsafe {
self.buffer_ref
.receive(data, furi::time::FuriDuration::ZERO)
}
}

/// Receive bytes, blocking if necessary.
Expand All @@ -451,7 +454,7 @@ mod stream {
pub fn recv_blocking(&self, data: &mut [u8]) -> usize {
unsafe {
self.buffer_ref
.receive(data, furi::time::Duration::WAIT_FOREVER)
.receive(data, furi::time::FuriDuration::WAIT_FOREVER)
}
}

Expand All @@ -464,7 +467,11 @@ mod stream {
/// # Interrupt Routines
///
/// In an interrupt routine, this method behaves like [`recv`](Self::recv).
pub fn recv_with_timeout(&self, data: &mut [u8], timeout: furi::time::Duration) -> usize {
pub fn recv_with_timeout(
&self,
data: &mut [u8],
timeout: furi::time::FuriDuration,
) -> usize {
unsafe { self.buffer_ref.receive(data, timeout) }
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/flipperzero/src/furi/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use flipperzero_sys as sys;
use lock_api::{GuardNoSend, RawMutex, RawMutexTimed};
use sys::furi::Status;

use super::time::{Duration, Instant};
use super::time::{FuriDuration, FuriInstant};

const MUTEX_TYPE: sys::FuriMutexType = sys::FuriMutexTypeNormal;

Expand Down Expand Up @@ -88,15 +88,15 @@ unsafe impl RawMutex for FuriMutex {
}

unsafe impl RawMutexTimed for FuriMutex {
type Duration = Duration;
type Instant = Instant;
type Duration = FuriDuration;
type Instant = FuriInstant;

fn try_lock_for(&self, timeout: Self::Duration) -> bool {
self.try_acquire(timeout.0)
}

fn try_lock_until(&self, timeout: Self::Instant) -> bool {
let now = Instant::now();
let now = FuriInstant::now();
self.try_lock_for(timeout - now)
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/flipperzero/src/furi/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use alloc::{

use flipperzero_sys::{self as sys, FuriFlagNoClear, FuriFlagWaitAll, FuriFlagWaitAny, HasFlag};

use crate::furi::time::Duration;
use crate::furi::time::FuriDuration;

#[cfg(feature = "alloc")]
const MIN_STACK_SIZE: usize = 1024;
Expand Down Expand Up @@ -226,7 +226,7 @@ pub fn sleep(duration: core::time::Duration) {
/// The maximum supported duration is `2^32` ticks (system timer dependent).
///
/// See [`sleep`] to sleep based on arbitary duration.
pub fn sleep_ticks(duration: Duration) {
pub fn sleep_ticks(duration: FuriDuration) {
unsafe {
sys::furi_delay_tick(duration.as_ticks());
}
Expand Down Expand Up @@ -296,7 +296,7 @@ pub fn get_flags() -> Result<u32, sys::furi::Status> {
pub fn wait_any_flags(
flags: u32,
clear: bool,
timeout: Duration,
timeout: FuriDuration,
) -> Result<u32, sys::furi::Status> {
let options = FuriFlagWaitAny.0 | (if clear { 0 } else { FuriFlagNoClear.0 });
let result = unsafe { sys::furi_thread_flags_wait(flags, options, timeout.0) };
Expand All @@ -314,7 +314,7 @@ pub fn wait_any_flags(
pub fn wait_all_flags(
flags: u32,
clear: bool,
timeout: Duration,
timeout: FuriDuration,
) -> Result<u32, sys::furi::Status> {
let options = FuriFlagWaitAll.0 | (if clear { 0 } else { FuriFlagNoClear.0 });
let result = unsafe { sys::furi_thread_flags_wait(flags, options, timeout.0) };
Expand Down
Loading

0 comments on commit 3b29ef6

Please sign in to comment.