Skip to content

Commit

Permalink
Apply clippy changes in riscv-semihosting
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Oct 16, 2024
1 parent 483a012 commit fd77cf9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
1 change: 1 addition & 0 deletions riscv-semihosting/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Apply clippy changes
- Bump riscv dependency version
- Made `cfg` variable selection more robust for custom targets
- Fixed debug::exit() on riscv64 QEMU simulation
Expand Down
2 changes: 1 addition & 1 deletion riscv-semihosting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Given this, the
generally sufficient for using this library.

A major difference between this library and `cortex-m-semihosting` is that there
are mandatory features to choose the privilege level at which the semihosting
are features to choose the privilege level at which the semihosting
calls are executed. The *machine-mode (M-mode)* feature will cause the macros in `export`
to execute the semihosting operation in an interrupt-free context, while
*user-mode (U-mode)* causes them to just execute the operation.
Expand Down
70 changes: 40 additions & 30 deletions riscv-semihosting/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! IMPLEMENTATION DETAILS USED BY MACROS
use crate::hio::{self, HostStream};
use core::fmt::{self, Write};
use core::{
fmt::{self, Write},
ptr::addr_of_mut,
};

static mut HSTDOUT: Option<HostStream> = None;
static mut HSTDERR: Option<HostStream> = None;
Expand All @@ -11,42 +13,46 @@ mod machine {
use super::*;

pub fn hstdout_str(s: &str) {
let _result = critical_section::with(|_| unsafe {
if HSTDOUT.is_none() {
HSTDOUT = Some(hio::hstdout()?);
let _result = critical_section::with(|_| {
let hstdout = unsafe { &mut *addr_of_mut!(HSTDOUT) };
if hstdout.is_none() {
*hstdout = Some(hio::hstdout()?);
}

HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
hstdout.as_mut().unwrap().write_str(s).map_err(drop)
});
}

pub fn hstdout_fmt(args: fmt::Arguments) {
let _result = critical_section::with(|_| unsafe {
if HSTDOUT.is_none() {
HSTDOUT = Some(hio::hstdout()?);
let _result = critical_section::with(|_| {
let hstdout = unsafe { &mut *addr_of_mut!(HSTDOUT) };
if hstdout.is_none() {
*hstdout = Some(hio::hstdout()?);
}

HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
hstdout.as_mut().unwrap().write_fmt(args).map_err(drop)
});
}

pub fn hstderr_str(s: &str) {
let _result = critical_section::with(|_| unsafe {
if HSTDERR.is_none() {
HSTDERR = Some(hio::hstderr()?);
let _result = critical_section::with(|_| {
let hstderr = unsafe { &mut *addr_of_mut!(HSTDERR) };
if hstderr.is_none() {
*hstderr = Some(hio::hstderr()?);
}

HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
hstderr.as_mut().unwrap().write_str(s).map_err(drop)
});
}

pub fn hstderr_fmt(args: fmt::Arguments) {
let _result = critical_section::with(|_| unsafe {
if HSTDERR.is_none() {
HSTDERR = Some(hio::hstderr()?);
let _result = critical_section::with(|_| {
let hstderr = unsafe { &mut *addr_of_mut!(HSTDERR) };
if hstderr.is_none() {
*hstderr = Some(hio::hstderr()?);
}

HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
hstderr.as_mut().unwrap().write_fmt(args).map_err(drop)
});
}
}
Expand All @@ -59,41 +65,45 @@ mod user {

pub fn hstdout_str(s: &str) {
let _result = unsafe {
if HSTDOUT.is_none() {
HSTDOUT = Some(hio::hstdout().unwrap());
let hstdout = &mut *addr_of_mut!(HSTDOUT);
if hstdout.is_none() {
*hstdout = Some(hio::hstdout().unwrap());
}

HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
hstdout.as_mut().unwrap().write_str(s).map_err(drop)
};
}

pub fn hstdout_fmt(args: fmt::Arguments) {
let _result = unsafe {
if HSTDOUT.is_none() {
HSTDOUT = Some(hio::hstdout().unwrap());
let hstdout = &mut *addr_of_mut!(HSTDOUT);
if hstdout.is_none() {
*hstdout = Some(hio::hstdout().unwrap());
}

HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
hstdout.as_mut().unwrap().write_fmt(args).map_err(drop)
};
}

pub fn hstderr_str(s: &str) {
let _result = unsafe {
if HSTDERR.is_none() {
HSTDERR = Some(hio::hstderr().unwrap());
let hstderr = &mut *addr_of_mut!(HSTDERR);
if hstderr.is_none() {
*hstderr = Some(hio::hstderr().unwrap());
}

HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
hstderr.as_mut().unwrap().write_str(s).map_err(drop)
};
}

pub fn hstderr_fmt(args: fmt::Arguments) {
let _result = unsafe {
if HSTDERR.is_none() {
HSTDERR = Some(hio::hstderr().unwrap());
let hstderr = &mut *addr_of_mut!(HSTDERR);
if hstderr.is_none() {
*hstderr = Some(hio::hstderr().unwrap());
}

HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
hstderr.as_mut().unwrap().write_fmt(args).map_err(drop)
};
}
}
Expand Down

0 comments on commit fd77cf9

Please sign in to comment.