Ensure cargo fmt is run accross everything (#399)

I have fmt'd all the things!

- [x] Changelog updated / no changelog update needed
This commit is contained in:
Gwilym Inzani 2023-03-23 23:11:19 +00:00 committed by GitHub
commit aea8547f97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 106 additions and 47 deletions

View file

@ -28,9 +28,7 @@ impl FromStr for Colour {
fn from_str(colour: &str) -> Result<Self, Self::Err> { fn from_str(colour: &str) -> Result<Self, Self::Err> {
if colour.len() != 6 { if colour.len() != 6 {
return Err(format!( return Err(format!("Expected colour to be 6 characters, got {colour}"));
"Expected colour to be 6 characters, got {colour}"
));
} }
let r = u8::from_str_radix(&colour[0..2], 16).unwrap(); let r = u8::from_str_radix(&colour[0..2], 16).unwrap();

View file

@ -385,8 +385,7 @@ fn add_image_to_tile_data(
for j in inner_y * 8..inner_y * 8 + 8 { for j in inner_y * 8..inner_y * 8 + 8 {
for i in inner_x * 8..inner_x * 8 + 8 { for i in inner_x * 8..inner_x * 8 + 8 {
let colour = image.colour(x * tile_size + i, y * tile_size + j); let colour = image.colour(x * tile_size + i, y * tile_size + j);
tile_data tile_data.push(palette.colour_index(colour));
.push(palette.colour_index(colour));
} }
} }
} }

View file

@ -6,13 +6,10 @@ use agb::sync::Static;
#[agb::entry] #[agb::entry]
fn main(_gba: agb::Gba) -> ! { fn main(_gba: agb::Gba) -> ! {
let count = Static::new(0); let count = Static::new(0);
let _a = agb::interrupt::add_interrupt_handler( let _a = agb::interrupt::add_interrupt_handler(agb::interrupt::Interrupt::VBlank, |_| {
agb::interrupt::Interrupt::VBlank, let cur_count = count.read();
|_| { agb::println!("Hello, world, frame = {}", cur_count);
let cur_count = count.read(); count.write(cur_count + 1);
agb::println!("Hello, world, frame = {}", cur_count); });
count.write(cur_count + 1);
},
);
loop {} loop {}
} }

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]

View file

@ -49,4 +49,4 @@ pub(crate) fn dma3_exclusive<R>(f: impl FnOnce() -> R) -> R {
// returns the return value // returns the return value
ret ret
}) })
} }

View file

@ -3,8 +3,8 @@
//! EEPROM requires using DMA to issue commands for both reading and writing. //! EEPROM requires using DMA to issue commands for both reading and writing.
use crate::memory_mapped::MemoryMapped; use crate::memory_mapped::MemoryMapped;
use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess};
use crate::save::utils::Timeout; use crate::save::utils::Timeout;
use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess};
use core::cmp; use core::cmp;
const PORT: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0DFFFF00) }; const PORT: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0DFFFF00) };
@ -41,7 +41,10 @@ union BufferContents {
} }
impl BufferData { impl BufferData {
fn new() -> Self { fn new() -> Self {
BufferData { idx: 0, data: BufferContents { uninit: () } } BufferData {
idx: 0,
data: BufferContents { uninit: () },
}
} }
/// Writes a bit to the output buffer. /// Writes a bit to the output buffer.
@ -120,7 +123,10 @@ impl EepromProperties {
/// Writes a sector directly. /// Writes a sector directly.
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
fn write_sector_raw( fn write_sector_raw(
&self, word: usize, block: &[u8], timeout: &mut Timeout, &self,
word: usize,
block: &[u8],
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Write sector command. The command is a one bit, followed by a // Write sector command. The command is a one bit, followed by a
// zero bit, followed by the address, followed by 64 bits of data. // zero bit, followed by the address, followed by 64 bits of data.
@ -150,7 +156,11 @@ impl EepromProperties {
/// Writes a sector to the EEPROM, keeping any current contents outside the /// Writes a sector to the EEPROM, keeping any current contents outside the
/// buffer's range. /// buffer's range.
fn write_sector_safe( fn write_sector_safe(
&self, word: usize, data: &[u8], start: usize, timeout: &mut Timeout, &self,
word: usize,
data: &[u8],
start: usize,
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut buf = self.read_sector(word); let mut buf = self.read_sector(word);
buf[start..start + data.len()].copy_from_slice(data); buf[start..start + data.len()].copy_from_slice(data);
@ -159,7 +169,11 @@ impl EepromProperties {
/// Writes a sector to the EEPROM. /// Writes a sector to the EEPROM.
fn write_sector( fn write_sector(
&self, word: usize, data: &[u8], start: usize, timeout: &mut Timeout, &self,
word: usize,
data: &[u8],
start: usize,
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
if data.len() == 8 && start == 0 { if data.len() == 8 && start == 0 {
self.write_sector_raw(word, data, timeout) self.write_sector_raw(word, data, timeout)
@ -219,8 +233,14 @@ impl EepromProperties {
Ok(()) Ok(())
} }
} }
const PROPS_512B: EepromProperties = EepromProperties { addr_bits: 6, byte_len: 512 }; const PROPS_512B: EepromProperties = EepromProperties {
const PROPS_8K: EepromProperties = EepromProperties { addr_bits: 14, byte_len: 8 * 1024 }; addr_bits: 6,
byte_len: 512,
};
const PROPS_8K: EepromProperties = EepromProperties {
addr_bits: 14,
byte_len: 8 * 1024,
};
/// The [`RawSaveAccess`] used for 512 byte EEPROM. /// The [`RawSaveAccess`] used for 512 byte EEPROM.
pub struct Eeprom512B; pub struct Eeprom512B;

View file

@ -6,11 +6,11 @@
// TODO: Setup cartridge read timings for faster Flash access. // TODO: Setup cartridge read timings for faster Flash access.
use crate::memory_mapped::{MemoryMapped, MemoryMapped1DArray}; use crate::memory_mapped::{MemoryMapped, MemoryMapped1DArray};
use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess};
use crate::save::asm_utils::*; use crate::save::asm_utils::*;
use crate::save::utils::Timeout;
use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess};
use crate::sync::{InitOnce, Static}; use crate::sync::{InitOnce, Static};
use core::cmp; use core::cmp;
use crate::save::utils::Timeout;
// Volatile address ports for flash // Volatile address ports for flash
const FLASH_PORT_BANK: MemoryMapped<u8> = unsafe { MemoryMapped::new(0x0E000000) }; const FLASH_PORT_BANK: MemoryMapped<u8> = unsafe { MemoryMapped::new(0x0E000000) };
@ -314,7 +314,11 @@ impl ChipInfo {
/// Waits for a timeout, or an operation to complete. /// Waits for a timeout, or an operation to complete.
fn wait_for_timeout( fn wait_for_timeout(
&self, offset: usize, val: u8, ms: u16, timeout: &mut Timeout, &self,
offset: usize,
val: u8,
ms: u16,
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
timeout.start(); timeout.start();
let offset = 0x0E000000 + offset; let offset = 0x0E000000 + offset;
@ -371,7 +375,10 @@ impl ChipInfo {
/// Erases and writes an entire 128b sector on Atmel devices. /// Erases and writes an entire 128b sector on Atmel devices.
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
fn write_atmel_sector_raw( fn write_atmel_sector_raw(
&self, offset: usize, buf: &[u8], timeout: &mut Timeout, &self,
offset: usize,
buf: &[u8],
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
crate::interrupt::free(|_| { crate::interrupt::free(|_| {
issue_flash_command(CMD_WRITE); issue_flash_command(CMD_WRITE);
@ -387,12 +394,19 @@ impl ChipInfo {
/// case of non-sector aligned writes. /// case of non-sector aligned writes.
#[inline(never)] // avoid allocating the 128 byte buffer for no reason. #[inline(never)] // avoid allocating the 128 byte buffer for no reason.
fn write_atmel_sector_safe( fn write_atmel_sector_safe(
&self, offset: usize, buf: &[u8], start: usize, timeout: &mut Timeout, &self,
offset: usize,
buf: &[u8],
start: usize,
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut sector = [0u8; 128]; let mut sector = [0u8; 128];
self.read_buffer(offset, &mut sector[0..start])?; self.read_buffer(offset, &mut sector[0..start])?;
sector[start..start + buf.len()].copy_from_slice(buf); sector[start..start + buf.len()].copy_from_slice(buf);
self.read_buffer(offset + start + buf.len(), &mut sector[start + buf.len()..128])?; self.read_buffer(
offset + start + buf.len(),
&mut sector[start + buf.len()..128],
)?;
self.write_atmel_sector_raw(offset, &sector, timeout) self.write_atmel_sector_raw(offset, &sector, timeout)
} }
@ -401,7 +415,11 @@ impl ChipInfo {
/// ///
/// This avoids allocating stack if there is no need to. /// This avoids allocating stack if there is no need to.
fn write_atmel_sector( fn write_atmel_sector(
&self, offset: usize, buf: &[u8], start: usize, timeout: &mut Timeout, &self,
offset: usize,
buf: &[u8],
start: usize,
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
if start == 0 && buf.len() == 128 { if start == 0 && buf.len() == 128 {
self.write_atmel_sector_raw(offset, buf, timeout) self.write_atmel_sector_raw(offset, buf, timeout)
@ -433,7 +451,10 @@ impl RawSaveAccess for FlashAccess {
} }
fn prepare_write( fn prepare_write(
&self, sector: usize, count: usize, timeout: &mut Timeout, &self,
sector: usize,
count: usize,
timeout: &mut Timeout,
) -> Result<(), Error> { ) -> Result<(), Error> {
let chip = cached_chip_info()?; let chip = cached_chip_info()?;
chip.check_sector_len(sector, count)?; chip.check_sector_len(sector, count)?;

View file

@ -3,9 +3,9 @@
//! SRAM acts as ordinary memory mapped into the memory space, and as such //! SRAM acts as ordinary memory mapped into the memory space, and as such
//! is accessed using normal memory read/write commands. //! is accessed using normal memory read/write commands.
use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess};
use crate::save::asm_utils::*; use crate::save::asm_utils::*;
use crate::save::utils::Timeout; use crate::save::utils::Timeout;
use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess};
const SRAM_SIZE: usize = 32 * 1024; // 32 KiB const SRAM_SIZE: usize = 32 * 1024; // 32 KiB

View file

@ -2,7 +2,7 @@
use super::Error; use super::Error;
use crate::sync::{RawMutex, RawMutexGuard}; use crate::sync::{RawMutex, RawMutexGuard};
use crate::timer::{Timer, Divider}; use crate::timer::{Divider, Timer};
/// A timeout type used to prevent hardware errors in save media from hanging /// A timeout type used to prevent hardware errors in save media from hanging
/// the game. /// the game.

View file

@ -81,7 +81,10 @@ impl<T> Mutex<T> {
/// Creates a new lock containing a given value. /// Creates a new lock containing a given value.
#[must_use] #[must_use]
pub const fn new(t: T) -> Self { pub const fn new(t: T) -> Self {
Mutex { raw: RawMutex::new(), data: UnsafeCell::new(t) } Mutex {
raw: RawMutex::new(),
data: UnsafeCell::new(t),
}
} }
/// Returns a guard for this lock, or panics if there is another lock active. /// Returns a guard for this lock, or panics if there is another lock active.
@ -92,7 +95,10 @@ impl<T> Mutex<T> {
/// Returns a guard for this lock or `None` if there is another lock active. /// Returns a guard for this lock or `None` if there is another lock active.
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> { pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
if self.raw.raw_lock() { if self.raw.raw_lock() {
Some(MutexGuard { underlying: self, ptr: self.data.get() }) Some(MutexGuard {
underlying: self,
ptr: self.data.get(),
})
} else { } else {
None None
} }

View file

@ -1,6 +1,6 @@
use core::cmp;
use agb::save::{Error, MediaInfo}; use agb::save::{Error, MediaInfo};
use agb::sync::InitOnce; use agb::sync::InitOnce;
use core::cmp;
fn init_sram(gba: &mut agb::Gba) -> &'static MediaInfo { fn init_sram(gba: &mut agb::Gba) -> &'static MediaInfo {
static ONCE: InitOnce<MediaInfo> = InitOnce::new(); static ONCE: InitOnce<MediaInfo> = InitOnce::new();
@ -31,7 +31,11 @@ const MAX_BLOCK_SIZE: usize = 4 * 1024;
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
fn do_test( fn do_test(
gba: &mut agb::Gba, seed: Rng, offset: usize, len: usize, block_size: usize, gba: &mut agb::Gba,
seed: Rng,
offset: usize,
len: usize,
block_size: usize,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut buffer = [0; MAX_BLOCK_SIZE]; let mut buffer = [0; MAX_BLOCK_SIZE];
@ -61,9 +65,12 @@ fn do_test(
for i in 0..cur_len { for i in 0..cur_len {
let cur_byte = rng.next_u8(); let cur_byte = rng.next_u8();
assert_eq!( assert_eq!(
buffer[i], cur_byte, buffer[i],
cur_byte,
"Read does not match earlier write: {} != {} @ 0x{:05x}", "Read does not match earlier write: {} != {} @ 0x{:05x}",
buffer[i], cur_byte, current + i, buffer[i],
cur_byte,
current + i,
); );
} }
current += cur_len; current += cur_len;
@ -102,4 +109,4 @@ fn test_partial_writes(gba: &mut agb::Gba) {
do_test(gba, Rng(i * 10000), rand_offset, rand_length, block_size) do_test(gba, Rng(i * 10000), rand_offset, rand_length, block_size)
.expect("Test encountered error"); .expect("Test encountered error");
} }
} }

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]

View file

@ -34,6 +34,13 @@ _build_docs crate:
clean: clean:
just _all-crates _clean just _all-crates _clean
fmt:
just _all-crates _fmt
just _fmt tools
fmt-check:
just _all-crates _fmt-check
just _fmt-check tools
run-example example: run-example example:
just _build-example "{{example}}" just _build-example "{{example}}"
mgba-qt "$CARGO_TARGET_DIR/thumbv4t-none-eabi/debug/examples/{{example}}" mgba-qt "$CARGO_TARGET_DIR/thumbv4t-none-eabi/debug/examples/{{example}}"
@ -52,7 +59,7 @@ check-linker-script-consistency:
find -type f -name gba.ld -print0 | xargs -0 -n1 cmp -- agb/gba.ld find -type f -name gba.ld -print0 | xargs -0 -n1 cmp -- agb/gba.ld
find -type f -name gba_mb.ld -print0 | xargs -0 -n1 cmp -- agb/gba_mb.ld find -type f -name gba_mb.ld -print0 | xargs -0 -n1 cmp -- agb/gba_mb.ld
ci: check-linker-script-consistency build-debug clippy test build-release test-release doctest-agb build-roms build-book check-docs ci: check-linker-script-consistency build-debug clippy fmt-check test build-release test-release doctest-agb build-roms build-book check-docs
build-roms: build-roms:
just _build-rom "examples/the-purple-night" "PURPLENIGHT" just _build-rom "examples/the-purple-night" "PURPLENIGHT"
@ -94,7 +101,7 @@ _build-rom folder name:
TARGET_FOLDER="${CARGO_TARGET_DIR:-$GAME_FOLDER/target}" TARGET_FOLDER="${CARGO_TARGET_DIR:-$GAME_FOLDER/target}"
GBA_FILE="$TARGET_FOLDER/$GAME_NAME.gba" GBA_FILE="$TARGET_FOLDER/$GAME_NAME.gba"
(cd "$GAME_FOLDER" && cargo build --release --target thumbv4t-none-eabi && cargo clippy --release --target thumbv4t-none-eabi -- {{CLIPPY_ARGUMENTS}}) (cd "$GAME_FOLDER" && cargo build --release --target thumbv4t-none-eabi && cargo clippy --release --target thumbv4t-none-eabi -- {{CLIPPY_ARGUMENTS}} && cargo fmt --all -- --check)
mkdir -p examples/target/examples mkdir -p examples/target/examples
@ -127,6 +134,10 @@ _clippy crate:
(cd "{{crate}}" && cargo clippy --examples --tests -- {{CLIPPY_ARGUMENTS}}) (cd "{{crate}}" && cargo clippy --examples --tests -- {{CLIPPY_ARGUMENTS}})
_clean crate: _clean crate:
(cd "{{crate}}" && cargo clean) (cd "{{crate}}" && cargo clean)
_fmt crate:
(cd "{{crate}}" && cargo fmt --all)
_fmt-check crate:
(cd "{{crate}}" && cargo fmt --all -- --check)
_build-example example: _build-example example:
(cd agb && cargo build "--example={{example}}") (cd agb && cargo build "--example={{example}}")

View file

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = ["rust-src", "clippy"] components = ["rust-src", "clippy", "rustfmt"]