From d62fbae4f03c715ec3686af17e426ae73e188cdc Mon Sep 17 00:00:00 2001 From: Lokathor Date: Sun, 30 Dec 2018 23:29:58 -0700 Subject: [PATCH] more const! --- .travis.yml | 2 +- Cargo.toml | 4 ---- src/base/volatile.rs | 9 +++------ src/lib.rs | 3 +-- src/palram.rs | 12 ++++-------- 5 files changed, 9 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index f1b9af4..71c74d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ rust: before_script: - rustup component add rust-src - #- rustup component add clippy --toolchain=nightly + - rustup component add clippy --toolchain=nightly - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - (test -x $HOME/.cargo/bin/cargo-xbuild || cargo install cargo-xbuild) - (test -x $HOME/.cargo/bin/cargo-make || cargo install cargo-make) diff --git a/Cargo.toml b/Cargo.toml index 2e7a2d2..25d435d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,6 @@ publish = false typenum = "1.10" gba-proc-macro = "0.5" -#[dev-dependencies] -#quickcheck="0.7" -# TODO: F - [profile.release] lto = true panic = "abort" diff --git a/src/base/volatile.rs b/src/base/volatile.rs index 1ae8ae6..5d44beb 100644 --- a/src/base/volatile.rs +++ b/src/base/volatile.rs @@ -118,8 +118,7 @@ impl VolAddress { /// # Safety /// /// You must follow the standard safety rules as outlined in the type docs. - pub unsafe fn offset(self, offset: isize) -> Self { - // TODO: const this + pub const unsafe fn offset(self, offset: isize) -> Self { VolAddress { address: NonZeroUsize::new_unchecked(self.address.get().wrapping_add(offset as usize * core::mem::size_of::())), marker: PhantomData, @@ -135,8 +134,7 @@ impl VolAddress { /// from the operation or throw a `debug_assert!` or something instead of /// triggering UB. Eventually this will be `const fn`, which will potentially /// let you spot errors without even having to run your program. - pub fn is_aligned(self) -> bool { - // TODO: const this + pub const fn is_aligned(self) -> bool { self.address.get() % core::mem::align_of::() == 0 } @@ -276,8 +274,7 @@ impl VolAddressBlock { /// # Safety /// /// The slot given must be in bounds. - pub unsafe fn index_unchecked(self, slot: usize) -> VolAddress { - // TODO: const this + pub const unsafe fn index_unchecked(self, slot: usize) -> VolAddress { self.vol_address.offset(slot as isize) } diff --git a/src/lib.rs b/src/lib.rs index b6637d3..df36c72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,10 +2,9 @@ #![feature(asm)] #![feature(const_int_wrapping)] #![feature(const_int_rotate)] -#![feature(min_const_unsafe_fn)] -#![warn(missing_docs)] #![allow(clippy::cast_lossless)] #![deny(clippy::float_arithmetic)] +#![warn(missing_docs)] //! This crate helps you write GBA ROMs. //! diff --git a/src/palram.rs b/src/palram.rs index e5b251a..5158804 100644 --- a/src/palram.rs +++ b/src/palram.rs @@ -37,15 +37,13 @@ pub const PALRAM_BG: VolAddressBlock = unsafe { VolAddressBlock::new_unch pub const PALRAM_OBJ: VolAddressBlock = unsafe { VolAddressBlock::new_unchecked(VolAddress::new_unchecked(0x500_0200), 256) }; /// Obtains the address of the specified 8bpp background palette slot. -pub fn index_palram_bg_8bpp(slot: u8) -> VolAddress { - // TODO: const this +pub const fn index_palram_bg_8bpp(slot: u8) -> VolAddress { // Note(Lokathor): because of the `u8` limit we can't go out of bounds here. unsafe { PALRAM_BG.index_unchecked(slot as usize) } } /// Obtains the address of the specified 8bpp object palette slot. -pub fn index_palram_obj_8bpp(slot: u8) -> VolAddress { - // TODO: const this +pub const fn index_palram_obj_8bpp(slot: u8) -> VolAddress { // Note(Lokathor): because of the `u8` limit we can't go out of bounds here. unsafe { PALRAM_OBJ.index_unchecked(slot as usize) } } @@ -54,8 +52,7 @@ pub fn index_palram_obj_8bpp(slot: u8) -> VolAddress { /// /// Accesses `palbank * 16 + palslot`, if this is out of bounds the computation /// will wrap. -pub fn index_palram_bg_4bpp(palbank: u8, palslot: u8) -> VolAddress { - // TODO: const this +pub const fn index_palram_bg_4bpp(palbank: u8, palslot: u8) -> VolAddress { // Note(Lokathor): because of the `u8` limit we can't go out of bounds here. unsafe { PALRAM_BG.index_unchecked(palbank.wrapping_mul(16).wrapping_add(palslot) as usize) } } @@ -64,8 +61,7 @@ pub fn index_palram_bg_4bpp(palbank: u8, palslot: u8) -> VolAddress { /// /// Accesses `palbank * 16 + palslot`, if this is out of bounds the computation /// will wrap. -pub fn index_palram_obj_4bpp(palbank: u8, palslot: u8) -> VolAddress { - // TODO: const this +pub const fn index_palram_obj_4bpp(palbank: u8, palslot: u8) -> VolAddress { // Note(Lokathor): because of the `u8` limit we can't go out of bounds here. unsafe { PALRAM_OBJ.index_unchecked(palbank.wrapping_mul(16).wrapping_add(palslot) as usize) } }