From 85defe8e2b0eb7933bd73c97528b196b87fcc93d Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Thu, 23 Mar 2023 21:43:19 +0000 Subject: [PATCH 1/3] Add targets to format and check formatting --- justfile | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index 2f5a32a6..d901cce2 100644 --- a/justfile +++ b/justfile @@ -34,6 +34,13 @@ _build_docs crate: 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: just _build-example "{{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_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: just _build-rom "examples/the-purple-night" "PURPLENIGHT" @@ -94,7 +101,7 @@ _build-rom folder name: TARGET_FOLDER="${CARGO_TARGET_DIR:-$GAME_FOLDER/target}" 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 @@ -127,6 +134,10 @@ _clippy crate: (cd "{{crate}}" && cargo clippy --examples --tests -- {{CLIPPY_ARGUMENTS}}) _clean crate: (cd "{{crate}}" && cargo clean) +_fmt crate: + (cd "{{crate}}" && cargo fmt --all) +_fmt-check crate: + (cd "{{crate}}" && cargo fmt --all -- --check) _build-example example: (cd agb && cargo build "--example={{example}}") From ec3e43da56f7a7e9d4b89aa587e6f720b1ad34d2 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Thu, 23 Mar 2023 21:47:44 +0000 Subject: [PATCH 2/3] cargo fmt everywhere --- agb-image-converter/src/colour.rs | 4 +--- agb-image-converter/src/lib.rs | 3 +-- agb/examples/output.rs | 13 +++++------ agb/src/dma.rs | 2 +- agb/src/save/eeprom.rs | 34 ++++++++++++++++++++++------ agb/src/save/flash.rs | 37 ++++++++++++++++++++++++------- agb/src/save/sram.rs | 2 +- agb/src/save/utils.rs | 2 +- agb/src/sync/locks.rs | 10 +++++++-- agb/tests/save_test_common/mod.rs | 17 +++++++++----- 10 files changed, 86 insertions(+), 38 deletions(-) diff --git a/agb-image-converter/src/colour.rs b/agb-image-converter/src/colour.rs index e607cadd..26c64f6b 100644 --- a/agb-image-converter/src/colour.rs +++ b/agb-image-converter/src/colour.rs @@ -28,9 +28,7 @@ impl FromStr for Colour { fn from_str(colour: &str) -> Result { if colour.len() != 6 { - return Err(format!( - "Expected colour to be 6 characters, got {colour}" - )); + return Err(format!("Expected colour to be 6 characters, got {colour}")); } let r = u8::from_str_radix(&colour[0..2], 16).unwrap(); diff --git a/agb-image-converter/src/lib.rs b/agb-image-converter/src/lib.rs index 03800d53..257a53b5 100644 --- a/agb-image-converter/src/lib.rs +++ b/agb-image-converter/src/lib.rs @@ -385,8 +385,7 @@ fn add_image_to_tile_data( for j in inner_y * 8..inner_y * 8 + 8 { for i in inner_x * 8..inner_x * 8 + 8 { let colour = image.colour(x * tile_size + i, y * tile_size + j); - tile_data - .push(palette.colour_index(colour)); + tile_data.push(palette.colour_index(colour)); } } } diff --git a/agb/examples/output.rs b/agb/examples/output.rs index 0869f382..93b8595a 100644 --- a/agb/examples/output.rs +++ b/agb/examples/output.rs @@ -6,13 +6,10 @@ use agb::sync::Static; #[agb::entry] fn main(_gba: agb::Gba) -> ! { let count = Static::new(0); - let _a = agb::interrupt::add_interrupt_handler( - agb::interrupt::Interrupt::VBlank, - |_| { - let cur_count = count.read(); - agb::println!("Hello, world, frame = {}", cur_count); - count.write(cur_count + 1); - }, - ); + let _a = agb::interrupt::add_interrupt_handler(agb::interrupt::Interrupt::VBlank, |_| { + let cur_count = count.read(); + agb::println!("Hello, world, frame = {}", cur_count); + count.write(cur_count + 1); + }); loop {} } diff --git a/agb/src/dma.rs b/agb/src/dma.rs index 1bf1b0bd..7e92ede1 100644 --- a/agb/src/dma.rs +++ b/agb/src/dma.rs @@ -49,4 +49,4 @@ pub(crate) fn dma3_exclusive(f: impl FnOnce() -> R) -> R { // returns the return value ret }) -} \ No newline at end of file +} diff --git a/agb/src/save/eeprom.rs b/agb/src/save/eeprom.rs index 6511b201..08eaa537 100644 --- a/agb/src/save/eeprom.rs +++ b/agb/src/save/eeprom.rs @@ -3,8 +3,8 @@ //! EEPROM requires using DMA to issue commands for both reading and writing. use crate::memory_mapped::MemoryMapped; -use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess}; use crate::save::utils::Timeout; +use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess}; use core::cmp; const PORT: MemoryMapped = unsafe { MemoryMapped::new(0x0DFFFF00) }; @@ -41,7 +41,10 @@ union BufferContents { } impl BufferData { fn new() -> Self { - BufferData { idx: 0, data: BufferContents { uninit: () } } + BufferData { + idx: 0, + data: BufferContents { uninit: () }, + } } /// Writes a bit to the output buffer. @@ -120,7 +123,10 @@ impl EepromProperties { /// Writes a sector directly. #[allow(clippy::needless_range_loop)] fn write_sector_raw( - &self, word: usize, block: &[u8], timeout: &mut Timeout, + &self, + word: usize, + block: &[u8], + timeout: &mut Timeout, ) -> Result<(), Error> { // Write sector command. The command is a one bit, followed by a // 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 /// buffer's range. 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> { let mut buf = self.read_sector(word); buf[start..start + data.len()].copy_from_slice(data); @@ -159,7 +169,11 @@ impl EepromProperties { /// Writes a sector to the EEPROM. 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> { if data.len() == 8 && start == 0 { self.write_sector_raw(word, data, timeout) @@ -219,8 +233,14 @@ impl EepromProperties { Ok(()) } } -const PROPS_512B: EepromProperties = EepromProperties { addr_bits: 6, byte_len: 512 }; -const PROPS_8K: EepromProperties = EepromProperties { addr_bits: 14, byte_len: 8 * 1024 }; +const PROPS_512B: EepromProperties = EepromProperties { + 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. pub struct Eeprom512B; diff --git a/agb/src/save/flash.rs b/agb/src/save/flash.rs index 28b21e50..b160048a 100644 --- a/agb/src/save/flash.rs +++ b/agb/src/save/flash.rs @@ -6,11 +6,11 @@ // TODO: Setup cartridge read timings for faster Flash access. use crate::memory_mapped::{MemoryMapped, MemoryMapped1DArray}; -use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess}; use crate::save::asm_utils::*; +use crate::save::utils::Timeout; +use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess}; use crate::sync::{InitOnce, Static}; use core::cmp; -use crate::save::utils::Timeout; // Volatile address ports for flash const FLASH_PORT_BANK: MemoryMapped = unsafe { MemoryMapped::new(0x0E000000) }; @@ -314,7 +314,11 @@ impl ChipInfo { /// Waits for a timeout, or an operation to complete. 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> { timeout.start(); let offset = 0x0E000000 + offset; @@ -371,7 +375,10 @@ impl ChipInfo { /// Erases and writes an entire 128b sector on Atmel devices. #[allow(clippy::needless_range_loop)] fn write_atmel_sector_raw( - &self, offset: usize, buf: &[u8], timeout: &mut Timeout, + &self, + offset: usize, + buf: &[u8], + timeout: &mut Timeout, ) -> Result<(), Error> { crate::interrupt::free(|_| { issue_flash_command(CMD_WRITE); @@ -387,12 +394,19 @@ impl ChipInfo { /// case of non-sector aligned writes. #[inline(never)] // avoid allocating the 128 byte buffer for no reason. 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> { let mut sector = [0u8; 128]; self.read_buffer(offset, &mut sector[0..start])?; 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, §or, timeout) } @@ -401,7 +415,11 @@ impl ChipInfo { /// /// This avoids allocating stack if there is no need to. 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> { if start == 0 && buf.len() == 128 { self.write_atmel_sector_raw(offset, buf, timeout) @@ -433,7 +451,10 @@ impl RawSaveAccess for FlashAccess { } fn prepare_write( - &self, sector: usize, count: usize, timeout: &mut Timeout, + &self, + sector: usize, + count: usize, + timeout: &mut Timeout, ) -> Result<(), Error> { let chip = cached_chip_info()?; chip.check_sector_len(sector, count)?; diff --git a/agb/src/save/sram.rs b/agb/src/save/sram.rs index 614b751e..2a7c7985 100644 --- a/agb/src/save/sram.rs +++ b/agb/src/save/sram.rs @@ -3,9 +3,9 @@ //! SRAM acts as ordinary memory mapped into the memory space, and as such //! is accessed using normal memory read/write commands. -use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess}; use crate::save::asm_utils::*; use crate::save::utils::Timeout; +use crate::save::{Error, MediaInfo, MediaType, RawSaveAccess}; const SRAM_SIZE: usize = 32 * 1024; // 32 KiB diff --git a/agb/src/save/utils.rs b/agb/src/save/utils.rs index b94cb3fe..ea12ee27 100644 --- a/agb/src/save/utils.rs +++ b/agb/src/save/utils.rs @@ -2,7 +2,7 @@ use super::Error; 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 /// the game. diff --git a/agb/src/sync/locks.rs b/agb/src/sync/locks.rs index a1a0cf3f..fdb7033f 100644 --- a/agb/src/sync/locks.rs +++ b/agb/src/sync/locks.rs @@ -81,7 +81,10 @@ impl Mutex { /// Creates a new lock containing a given value. #[must_use] 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. @@ -92,7 +95,10 @@ impl Mutex { /// Returns a guard for this lock or `None` if there is another lock active. pub fn try_lock(&self) -> Option> { if self.raw.raw_lock() { - Some(MutexGuard { underlying: self, ptr: self.data.get() }) + Some(MutexGuard { + underlying: self, + ptr: self.data.get(), + }) } else { None } diff --git a/agb/tests/save_test_common/mod.rs b/agb/tests/save_test_common/mod.rs index d3f823c2..3f7c2049 100644 --- a/agb/tests/save_test_common/mod.rs +++ b/agb/tests/save_test_common/mod.rs @@ -1,6 +1,6 @@ -use core::cmp; use agb::save::{Error, MediaInfo}; use agb::sync::InitOnce; +use core::cmp; fn init_sram(gba: &mut agb::Gba) -> &'static MediaInfo { static ONCE: InitOnce = InitOnce::new(); @@ -31,7 +31,11 @@ const MAX_BLOCK_SIZE: usize = 4 * 1024; #[allow(clippy::needless_range_loop)] 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> { let mut buffer = [0; MAX_BLOCK_SIZE]; @@ -61,9 +65,12 @@ fn do_test( for i in 0..cur_len { let cur_byte = rng.next_u8(); assert_eq!( - buffer[i], cur_byte, + buffer[i], + cur_byte, "Read does not match earlier write: {} != {} @ 0x{:05x}", - buffer[i], cur_byte, current + i, + buffer[i], + cur_byte, + current + i, ); } 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) .expect("Test encountered error"); } -} \ No newline at end of file +} From 8a3660733dd10f4ce1f6b63e92895eb191f4da0d Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Thu, 23 Mar 2023 22:17:02 +0000 Subject: [PATCH 3/3] Add rustfmt as a component to rust-toolchain --- agb/rust-toolchain.toml | 2 +- book/games/pong/rust-toolchain.toml | 2 +- examples/combo/rust-toolchain.toml | 2 +- examples/hyperspace-roll/rust-toolchain.toml | 2 +- examples/the-hat-chooses-the-wizard/rust-toolchain.toml | 2 +- examples/the-purple-night/rust-toolchain.toml | 2 +- template/rust-toolchain.toml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/agb/rust-toolchain.toml b/agb/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/agb/rust-toolchain.toml +++ b/agb/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"] diff --git a/book/games/pong/rust-toolchain.toml b/book/games/pong/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/book/games/pong/rust-toolchain.toml +++ b/book/games/pong/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"] diff --git a/examples/combo/rust-toolchain.toml b/examples/combo/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/examples/combo/rust-toolchain.toml +++ b/examples/combo/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"] diff --git a/examples/hyperspace-roll/rust-toolchain.toml b/examples/hyperspace-roll/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/examples/hyperspace-roll/rust-toolchain.toml +++ b/examples/hyperspace-roll/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"] diff --git a/examples/the-hat-chooses-the-wizard/rust-toolchain.toml b/examples/the-hat-chooses-the-wizard/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/examples/the-hat-chooses-the-wizard/rust-toolchain.toml +++ b/examples/the-hat-chooses-the-wizard/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"] diff --git a/examples/the-purple-night/rust-toolchain.toml b/examples/the-purple-night/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/examples/the-purple-night/rust-toolchain.toml +++ b/examples/the-purple-night/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"] diff --git a/template/rust-toolchain.toml b/template/rust-toolchain.toml index 06842486..6e1f5327 100644 --- a/template/rust-toolchain.toml +++ b/template/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "nightly" -components = ["rust-src", "clippy"] \ No newline at end of file +components = ["rust-src", "clippy", "rustfmt"]