@@ -20,7 +20,7 @@ use core::{cell::RefCell, convert::Infallible, sync::atomic::AtomicBool};
2020use critical_section:: Mutex ;
2121use embassy_sync:: waitqueue:: AtomicWaker ;
2222use embedded_hal_async:: delay:: DelayNs ;
23- use raw_slice :: RawBufSlice ;
23+ use raw_buffer :: RawBufSlice ;
2424
2525use crate :: {
2626 FIFO_DEPTH , Tx ,
@@ -130,23 +130,15 @@ impl TxContext {
130130}
131131
132132/// TX future structure.
133- pub struct TxFuture {
133+ pub struct TxFuture < ' tx , ' buf > {
134134 waker_idx : usize ,
135135 reg_block : registers:: MmioRegisters < ' static > ,
136+ phantom : core:: marker:: PhantomData < ( & ' tx ( ) , & ' buf ( ) ) > ,
136137}
137138
138- impl TxFuture {
139+ impl < ' tx , ' buf > TxFuture < ' tx , ' buf > {
139140 /// Create a new TX future which can be used for asynchronous TX operations.
140- ///
141- /// # Safety
142- ///
143- /// This function stores the raw pointer of the passed data slice. The user MUST ensure
144- /// that the slice outlives the data structure.
145- pub unsafe fn new (
146- tx : & mut Tx ,
147- waker_idx : usize ,
148- data : & [ u8 ] ,
149- ) -> Result < Self , InvalidWakerIndex > {
141+ pub fn new ( tx : & mut Tx , waker_idx : usize , data : & ' buf [ u8 ] ) -> Result < Self , InvalidWakerIndex > {
150142 TX_DONE [ waker_idx] . store ( false , core:: sync:: atomic:: Ordering :: Relaxed ) ;
151143 tx. disable_interrupt ( ) ;
152144 tx. reset_fifo ( ) ;
@@ -168,11 +160,12 @@ impl TxFuture {
168160 Ok ( Self {
169161 waker_idx,
170162 reg_block : unsafe { tx. regs . clone ( ) } ,
163+ phantom : core:: marker:: PhantomData ,
171164 } )
172165 }
173166}
174167
175- impl Future for TxFuture {
168+ impl Future for TxFuture < ' _ , ' _ > {
176169 type Output = usize ;
177170
178171 fn poll (
@@ -192,7 +185,7 @@ impl Future for TxFuture {
192185 }
193186}
194187
195- impl Drop for TxFuture {
188+ impl Drop for TxFuture < ' _ , ' _ > {
196189 fn drop ( & mut self ) {
197190 let mut tx = Tx :: new ( unsafe { self . reg_block . clone ( ) } ) ;
198191 tx. disable_interrupt ( ) ;
@@ -212,7 +205,12 @@ impl<D: DelayNs> TxAsync<D> {
212205 /// The delay function is a [DelayNs] provider which is used to allow flushing the
213206 /// device properly. This is because even when a write finished, the UART might still
214207 /// be busy shifting the last byte out.
215- pub fn new ( tx : Tx , waker_idx : usize , delay : D ) -> Result < Self , InvalidWakerIndex > {
208+ ///
209+ /// # Safety
210+ ///
211+ /// The user MUST ensure that the `Drop` method of all futures generated with this driver
212+ /// is called on transfer cancellation. By default, this does not require any special handling.
213+ pub unsafe fn new ( tx : Tx , waker_idx : usize , delay : D ) -> Result < Self , InvalidWakerIndex > {
216214 if waker_idx >= NUM_WAKERS {
217215 return Err ( InvalidWakerIndex ( waker_idx) ) ;
218216 }
@@ -227,12 +225,8 @@ impl<D: DelayNs> TxAsync<D> {
227225 ///
228226 /// This implementation is not side effect free, and a started future might have already
229227 /// written part of the passed buffer.
230- pub async fn write ( & mut self , buf : & [ u8 ] ) -> usize {
231- if buf. is_empty ( ) {
232- return 0 ;
233- }
234- let fut = unsafe { TxFuture :: new ( & mut self . tx , self . waker_idx , buf) . unwrap ( ) } ;
235- fut. await
228+ pub fn write < ' buf > ( & mut self , buf : & ' buf [ u8 ] ) -> TxFuture < ' _ , ' buf > {
229+ TxFuture :: new ( & mut self . tx , self . waker_idx , buf) . unwrap ( )
236230 }
237231
238232 /// Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
0 commit comments