mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
Split_ref with shortened lifetime. When borrowed skip drop on rx and tx
This commit is contained in:
parent
aa453caa79
commit
72109a7bda
@ -157,6 +157,7 @@ pub struct BufferedUartTx<'d> {
|
||||
tx: Option<PeripheralRef<'d, AnyPin>>,
|
||||
cts: Option<PeripheralRef<'d, AnyPin>>,
|
||||
de: Option<PeripheralRef<'d, AnyPin>>,
|
||||
is_borrowed: bool,
|
||||
}
|
||||
|
||||
/// Rx-only buffered UART
|
||||
@ -168,6 +169,7 @@ pub struct BufferedUartRx<'d> {
|
||||
kernel_clock: Hertz,
|
||||
rx: Option<PeripheralRef<'d, AnyPin>>,
|
||||
rts: Option<PeripheralRef<'d, AnyPin>>,
|
||||
is_borrowed: bool,
|
||||
}
|
||||
|
||||
impl<'d> SetConfig for BufferedUart<'d> {
|
||||
@ -341,6 +343,7 @@ impl<'d> BufferedUart<'d> {
|
||||
kernel_clock,
|
||||
rx,
|
||||
rts,
|
||||
is_borrowed: false,
|
||||
},
|
||||
tx: BufferedUartTx {
|
||||
info,
|
||||
@ -349,6 +352,7 @@ impl<'d> BufferedUart<'d> {
|
||||
tx,
|
||||
cts,
|
||||
de,
|
||||
is_borrowed: false,
|
||||
},
|
||||
};
|
||||
this.enable_and_configure(tx_buffer, rx_buffer, &config)?;
|
||||
@ -396,11 +400,29 @@ impl<'d> BufferedUart<'d> {
|
||||
(self.tx, self.rx)
|
||||
}
|
||||
|
||||
/// Split the Uart into a transmitter and receiver by mutable reference,
|
||||
/// Split the Uart into a transmitter and receiver,
|
||||
/// which is particularly useful when having two tasks correlating to
|
||||
/// transmitting and receiving.
|
||||
pub fn split_ref(&mut self) -> (&mut BufferedUartTx<'d>, &mut BufferedUartRx<'d>) {
|
||||
(&mut self.tx, &mut self.rx)
|
||||
pub fn split_ref(&mut self) -> (BufferedUartTx<'_>, BufferedUartRx<'_>) {
|
||||
(
|
||||
BufferedUartTx {
|
||||
info: self.tx.info,
|
||||
state: self.tx.state,
|
||||
kernel_clock: self.tx.kernel_clock,
|
||||
tx: self.tx.tx.as_mut().map(PeripheralRef::reborrow),
|
||||
cts: self.tx.cts.as_mut().map(PeripheralRef::reborrow),
|
||||
de: self.tx.de.as_mut().map(PeripheralRef::reborrow),
|
||||
is_borrowed: true,
|
||||
},
|
||||
BufferedUartRx {
|
||||
info: self.rx.info,
|
||||
state: self.rx.state,
|
||||
kernel_clock: self.rx.kernel_clock,
|
||||
rx: self.rx.rx.as_mut().map(PeripheralRef::reborrow),
|
||||
rts: self.rx.rts.as_mut().map(PeripheralRef::reborrow),
|
||||
is_borrowed: true,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Reconfigure the driver
|
||||
@ -607,6 +629,7 @@ impl<'d> BufferedUartTx<'d> {
|
||||
|
||||
impl<'d> Drop for BufferedUartRx<'d> {
|
||||
fn drop(&mut self) {
|
||||
if !self.is_borrowed {
|
||||
let state = self.state;
|
||||
unsafe {
|
||||
state.rx_buf.deinit();
|
||||
@ -623,9 +646,11 @@ impl<'d> Drop for BufferedUartRx<'d> {
|
||||
drop_tx_rx(self.info, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d> Drop for BufferedUartTx<'d> {
|
||||
fn drop(&mut self) {
|
||||
if !self.is_borrowed {
|
||||
let state = self.state;
|
||||
unsafe {
|
||||
state.tx_buf.deinit();
|
||||
@ -643,6 +668,7 @@ impl<'d> Drop for BufferedUartTx<'d> {
|
||||
drop_tx_rx(self.info, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn drop_tx_rx(info: &Info, state: &State) {
|
||||
// We cannot use atomic subtraction here, because it's not supported for all targets
|
||||
|
Loading…
Reference in New Issue
Block a user