Skip to content

Commit

Permalink
Fix all clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatekii committed Mar 3, 2023
1 parent e07ef0a commit c72dec6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
5 changes: 3 additions & 2 deletions rtt-target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl UpChannel {
UpChannel(channel)
}

#[allow(clippy::mut_from_ref)]
fn channel(&self) -> &mut rtt::RttChannel {
unsafe { &mut *self.0 }
}
Expand Down Expand Up @@ -234,15 +235,15 @@ impl UpChannel {
static mut CONTROL_BLOCK: MaybeUninit<rtt::RttHeader>;
}

if number >= (&*CONTROL_BLOCK.as_ptr()).max_up_channels() {
if number >= (*CONTROL_BLOCK.as_ptr()).max_up_channels() {
return None;
}

// First addition moves to the start of the up channel array, second addition moves to the
// correct channel.
let ptr = (CONTROL_BLOCK.as_ptr().add(1) as *mut rtt::RttChannel).add(number);

if !(&*ptr).is_initialized() {
if !(*ptr).is_initialized() {
return None;
}

Expand Down
2 changes: 1 addition & 1 deletion rtt-target/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn set_print_channel(channel: UpChannel) {
pub mod print_impl {
use super::*;

fn with_writer<F: Fn(TerminalWriter) -> ()>(number: u8, f: F) {
fn with_writer<F: Fn(TerminalWriter)>(number: u8, f: F) {
critical_section::with(|cs| {
if let Some(term) = &mut *PRINT_TERMINAL.borrow_ref_mut(cs) {
f(term.write(number))
Expand Down
28 changes: 10 additions & 18 deletions rtt-target/src/rtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ impl RttChannel {
/// The pointer arguments must point to a valid null-terminated name and writable buffer.
pub unsafe fn init(&mut self, name: *const u8, mode: ChannelMode, buffer: *mut [u8]) {
ptr::write_volatile(&mut self.name, name);
ptr::write_volatile(&mut self.size, (&*buffer).len());
ptr::write_volatile(&mut self.size, (*buffer).len());
self.set_mode(mode);

// Set buffer last as it can be used to detect if the channel has been initialized
ptr::write_volatile(&mut self.buffer, buffer as *mut u8);
}

pub fn is_initialized(&self) -> bool {
self.buffer != ptr::null_mut()
self.buffer.is_null()
}

pub(crate) fn mode(&self) -> ChannelMode {
Expand All @@ -100,18 +100,14 @@ impl RttChannel {
let mut total = 0;

// Read while buffer contains data and output buffer has space (maximum of two iterations)
while buf.len() > 0 {
while !buf.is_empty() {
let count = min(self.readable_contiguous(write, read), buf.len());
if count == 0 {
break;
}

unsafe {
ptr::copy_nonoverlapping(
self.buffer.offset(read as isize),
buf.as_mut_ptr(),
count,
);
ptr::copy_nonoverlapping(self.buffer.add(read), buf.as_mut_ptr(), count);
}

total += count;
Expand Down Expand Up @@ -142,11 +138,11 @@ impl RttChannel {

/// Gets the amount of contiguous data available for reading
fn readable_contiguous(&self, write: usize, read: usize) -> usize {
(if read > write {
if read > write {
self.size - read
} else {
write - read
}) as usize
}
}

fn read_pointers(&self) -> (usize, usize) {
Expand Down Expand Up @@ -221,11 +217,7 @@ impl RttWriter<'_> {
}

unsafe {
ptr::copy_nonoverlapping(
buf.as_ptr(),
self.chan.buffer.offset(self.write as isize),
count,
);
ptr::copy_nonoverlapping(buf.as_ptr(), self.chan.buffer.add(self.write), count);
}

self.write += count;
Expand All @@ -244,13 +236,13 @@ impl RttWriter<'_> {
fn writable_contiguous(&self) -> usize {
let read = self.chan.read_pointers().1;

(if read > self.write {
if read > self.write {
read - self.write - 1
} else if read == 0 {
self.chan.size - self.write - 1
} else {
self.chan.size - self.write
}) as usize
}
}

pub fn is_failed(&self) -> bool {
Expand All @@ -265,7 +257,7 @@ impl RttWriter<'_> {

fn commit_impl(&mut self) {
match self.state {
WriteState::Finished => return,
WriteState::Finished => (),
WriteState::Full | WriteState::Writable => {
// Commit the write pointer so the host can see the new data
self.chan.write.store(self.write, SeqCst);
Expand Down

0 comments on commit c72dec6

Please sign in to comment.