Skip to content

Commit

Permalink
fix various clippy warnings (#51)
Browse files Browse the repository at this point in the history
* fix clippy unneeded reference warnings

* remove `let` bindings with unit values

* add error types (clippy hates `Result<_, ()>`)

* remove unused lifetimes

* add safety docs

* rustfmt
  • Loading branch information
hawkw authored Jun 2, 2023
1 parent 3e7b70e commit ddfa78a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion source/kernel/src/comms/kchannel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl<T> KConsumer<T> {

impl Clone for ErasedKProducer {
fn clone(&self) -> Self {
unsafe { (self.cloner)(&self) }
unsafe { (self.cloner)(self) }
}
}

Expand Down
9 changes: 7 additions & 2 deletions source/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,13 @@ impl Kernel {
F: Future + 'static,
{
let task = self.new_task(fut);
let mut guard = self.heap().lock().map_err(drop)?;
let task_box = guard.alloc_box(task).map_err(drop)?;
let mut guard = self
.heap()
.lock()
.map_err(|_| "kernel heap already locked")?;
let task_box = guard
.alloc_box(task)
.map_err(|_| "could not allocate task storage")?;
Ok(self.spawn_allocated(task_box))
}

Expand Down
17 changes: 7 additions & 10 deletions source/kernel/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ impl Registry {
if self.items.iter().any(|i| i.key == RD::UUID) {
return Err(RegistrationError::UuidAlreadyRegistered);
}
let result = self
.items
self.items
.push(RegistryItem {
key: RD::UUID,
value: RegistryValue {
Expand All @@ -327,7 +326,7 @@ impl Registry {
.map_err(|_| RegistrationError::RegistryFull)?;
info!(uuid = ?RD::UUID, service_id = self.counter, "Registered KOnly");
self.counter = self.counter.wrapping_add(1);
Ok(result)
Ok(())
}

/// Register a driver service for use in the kernel (including drivers) as
Expand All @@ -350,8 +349,7 @@ impl Registry {
if self.items.iter().any(|i| i.key == RD::UUID) {
return Err(RegistrationError::UuidAlreadyRegistered);
}
let result = self
.items
self.items
.push(RegistryItem {
key: RD::UUID,
value: RegistryValue {
Expand All @@ -364,7 +362,7 @@ impl Registry {
.map_err(|_| RegistrationError::RegistryFull)?;
info!(uuid = ?RD::UUID, service_id = self.counter, "Registered");
self.counter = self.counter.wrapping_add(1);
Ok(result)
Ok(())
}

/// Get a kernelspace (including drivers) handle of a given driver service.
Expand Down Expand Up @@ -419,7 +417,7 @@ impl Registry {
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
let item = self.items.iter().find(|i| &i.key == &RD::UUID)?;
let item = self.items.iter().find(|i| i.key == RD::UUID)?;
let client_id = self.counter;
info!(uuid = ?RD::UUID, service_id = item.value.service_id.0, client_id = self.counter, "Got KernelHandle from Registry");
self.counter = self.counter.wrapping_add(1);
Expand Down Expand Up @@ -553,8 +551,7 @@ impl<RD: RegisteredDriver> KernelHandle<RD> {
pub async fn send(&mut self, msg: RD::Request, reply: ReplyTo<RD>) -> Result<(), ()> {
let request_id = RequestResponseId::new(self.request_ctr, MessageKind::Request);
self.request_ctr = self.request_ctr.wrapping_add(1);
let result = self
.prod
self.prod
.enqueue_async(Message {
msg: Envelope {
body: msg,
Expand All @@ -572,7 +569,7 @@ impl<RD: RegisteredDriver> KernelHandle<RD> {
request_id = request_id.id(),
"Sent Request"
);
Ok(result)
Ok(())
}
}

Expand Down
4 changes: 4 additions & 0 deletions source/mstd/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ where
}

impl Terpsichore {
/// # Safety
///
/// - `heap_start` must be a valid pointer to a region of memory of size `heap_len`.
/// - The region of memory pointed to by `heap_start` must not be mutably aliased.
// TODO: This is *probably* something that needs to be called by the entrypoint, which
// might be provided per-platform.
//
Expand Down
28 changes: 23 additions & 5 deletions source/mstd/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::{
cell::UnsafeCell,
fmt,
marker::PhantomData,
ops::{Deref, DerefMut},
ptr::NonNull,
Expand All @@ -15,6 +16,11 @@ pub struct ArfCell<T> {
item: UnsafeCell<T>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BorrowError {
mutable: bool,
}

unsafe impl<T> Sync for ArfCell<T> where T: Send {}

pub struct MutArfGuard<'a, T> {
Expand Down Expand Up @@ -76,7 +82,7 @@ impl<T> ArfCell<T> {
}
}

pub fn borrow_mut<'a>(&'a self) -> Result<MutArfGuard<'a, T>, ()> {
pub fn borrow_mut(&self) -> Result<MutArfGuard<'_, T>, BorrowError> {
self.state
.compare_exchange(
0,
Expand All @@ -85,18 +91,18 @@ impl<T> ArfCell<T> {
Ordering::SeqCst,
Ordering::SeqCst,
)
.map_err(drop)?;
.map_err(|_| BorrowError { mutable: false })?;

Ok(MutArfGuard {
cell: unsafe { NonNull::new_unchecked(self as *const Self as *mut Self) },
plt: PhantomData,
})
}

pub fn borrow<'a>(&'a self) -> Result<ArfGuard<'a, T>, ()> {
pub fn borrow(&self) -> Result<ArfGuard<'_, T>, BorrowError> {
// proactive check we aren't mutably locked
if self.state.load(Ordering::Acquire) >= Self::MUTLOCK {
return Err(());
return Err(BorrowError { mutable: true });
}

// TODO: Check the old value to see if we're close to overflowing the refcnt?
Expand All @@ -109,7 +115,7 @@ impl<T> ArfCell<T> {
// unconditionally reset to zero, and future borrowers will hopefully be
// caught by the proactive check above to reduce the chance of
// overflowing the refcount.
return Err(());
return Err(BorrowError { mutable: true });
}

Ok(ArfGuard {
Expand All @@ -118,3 +124,15 @@ impl<T> ArfCell<T> {
})
}
}

// === impl BorrowError ===

impl fmt::Display for BorrowError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.mutable {
write!(f, "already mutably borrowed")
} else {
write!(f, "already borrowed")
}
}
}
2 changes: 1 addition & 1 deletion tools/crowtty/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut nmsg = Vec::new();
nmsg.extend_from_slice(&port_idx.to_le_bytes());
nmsg.extend_from_slice(&msg);
let mut enc_msg = cobs::encode_vec(&mut nmsg);
let mut enc_msg = cobs::encode_vec(&nmsg);
enc_msg.push(0);
println!("Sending {} bytes to port {}", enc_msg.len(), port_idx);
port.write_all(&enc_msg)?;
Expand Down

0 comments on commit ddfa78a

Please sign in to comment.