diff --git a/Cargo.lock b/Cargo.lock index 4a05828f5..433367be1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,7 +85,6 @@ dependencies = [ "ostd", "smoltcp", "spin", - "static_assertions", "takeable", ] @@ -100,7 +99,6 @@ dependencies = [ "log", "ostd", "spin", - "static_assertions", ] [[package]] @@ -162,7 +160,6 @@ dependencies = [ "ostd-pod", "postcard", "serde", - "static_assertions", ] [[package]] @@ -219,7 +216,6 @@ dependencies = [ "rand", "riscv", "spin", - "static_assertions", "takeable", "tdx-guest", "time", @@ -1296,7 +1292,6 @@ dependencies = [ "sbi-rt", "smallvec", "spin", - "static_assertions", "tdx-guest", "unwinding", "volatile 0.6.1", @@ -1632,12 +1627,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "strsim" version = "0.10.0" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index c383a34ec..b35af2096 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -52,7 +52,6 @@ rand = { version = "0.8.5", default-features = false, features = [ "small_rng", "std_rng", ] } -static_assertions = "1.1.0" inherit-methods-macro = { git = "https://github.com/asterinas/inherit-methods-macro", rev = "98f7e3e" } getset = "0.1.2" takeable = "0.2.2" diff --git a/kernel/comps/block/Cargo.toml b/kernel/comps/block/Cargo.toml index d20f8a4c1..9d2832702 100644 --- a/kernel/comps/block/Cargo.toml +++ b/kernel/comps/block/Cargo.toml @@ -12,7 +12,6 @@ align_ext = { path = "../../../ostd/libs/align_ext" } int-to-c-enum = { path = "../../libs/int-to-c-enum" } component = { path = "../../libs/comp-sys/component" } log = "0.4" -static_assertions = "1.1.0" bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] } [lints] diff --git a/kernel/comps/block/src/id.rs b/kernel/comps/block/src/id.rs index 0ce474599..1385b44cc 100644 --- a/kernel/comps/block/src/id.rs +++ b/kernel/comps/block/src/id.rs @@ -5,8 +5,7 @@ use core::{ ops::{Add, Sub}, }; -use ostd::Pod; -use static_assertions::const_assert; +use ostd::{const_assert, Pod}; /// The block index used in the filesystem. pub type Bid = BlockId; diff --git a/kernel/comps/mlsdisk/Cargo.toml b/kernel/comps/mlsdisk/Cargo.toml index d9c521419..6086e34ff 100644 --- a/kernel/comps/mlsdisk/Cargo.toml +++ b/kernel/comps/mlsdisk/Cargo.toml @@ -26,7 +26,6 @@ log = "0.4" lru = "0.12.3" postcard = "1.0.6" serde = { version = "1.0.192", default-features = false, features = ["alloc", "derive"] } -static_assertions = "1.1.0" [lints] workspace = true diff --git a/kernel/comps/mlsdisk/src/layers/0-bio/mod.rs b/kernel/comps/mlsdisk/src/layers/0-bio/mod.rs index 9c6dbece3..f8da56760 100644 --- a/kernel/comps/mlsdisk/src/layers/0-bio/mod.rs +++ b/kernel/comps/mlsdisk/src/layers/0-bio/mod.rs @@ -2,13 +2,13 @@ //! The layer of untrusted block I/O. -use static_assertions::assert_eq_size; - mod block_buf; mod block_log; mod block_ring; mod block_set; +use ostd::const_assert; + pub use self::{ block_buf::{Buf, BufMut, BufRef}, block_log::{BlockLog, MemLog}, @@ -20,5 +20,5 @@ pub type BlockId = usize; pub const BLOCK_SIZE: usize = 0x1000; pub const BID_SIZE: usize = core::mem::size_of::(); -// This definition of BlockId assumes the target architecture is 64-bit -assert_eq_size!(usize, u64); +// This definition of `BlockId` assumes the target architecture is 64-bit. +const_assert!(BID_SIZE == 8); diff --git a/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_log.rs b/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_log.rs index b2f46930c..3bff7d0f7 100644 --- a/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_log.rs +++ b/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_log.rs @@ -3,9 +3,9 @@ use alloc::vec; use core::{any::Any, mem::size_of}; +use ostd::const_assert; use ostd_pod::Pod; use serde::{Deserialize, Serialize}; -use static_assertions::const_assert; use super::{Iv, Key, Mac}; use crate::{ diff --git a/kernel/libs/aster-bigtcp/Cargo.toml b/kernel/libs/aster-bigtcp/Cargo.toml index b7459d8ff..f188fd901 100644 --- a/kernel/libs/aster-bigtcp/Cargo.toml +++ b/kernel/libs/aster-bigtcp/Cargo.toml @@ -19,7 +19,6 @@ smoltcp = { git = "https://github.com/asterinas/smoltcp", tag = "r_2024-11-08_f0 "socket-tcp", ] } spin = "0.9.4" -static_assertions = "1.1.0" takeable = "0.2.2" [lints] diff --git a/kernel/libs/aster-bigtcp/src/socket_table.rs b/kernel/libs/aster-bigtcp/src/socket_table.rs index 52316b776..4244b099c 100644 --- a/kernel/libs/aster-bigtcp/src/socket_table.rs +++ b/kernel/libs/aster-bigtcp/src/socket_table.rs @@ -7,8 +7,8 @@ use alloc::{boxed::Box, sync::Arc, vec::Vec}; use core::net::Ipv4Addr; use jhash::{jhash_1vals, jhash_3vals}; +use ostd::const_assert; use smoltcp::wire::{IpAddress, IpEndpoint, IpListenEndpoint}; -use static_assertions::const_assert; use crate::{ ext::Ext, diff --git a/kernel/src/fs/ext2/block_group.rs b/kernel/src/fs/ext2/block_group.rs index be685c7e6..c30692488 100644 --- a/kernel/src/fs/ext2/block_group.rs +++ b/kernel/src/fs/ext2/block_group.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MPL-2.0 use id_alloc::IdAlloc; -use ostd::mm::UntypedMem; +use ostd::{const_assert, mm::UntypedMem}; use super::{ block_ptr::Ext2Bid, diff --git a/kernel/src/fs/ext2/inode.rs b/kernel/src/fs/ext2/inode.rs index 226b9acaa..329f21615 100644 --- a/kernel/src/fs/ext2/inode.rs +++ b/kernel/src/fs/ext2/inode.rs @@ -7,7 +7,7 @@ use alloc::{borrow::ToOwned, rc::Rc}; use core::sync::atomic::{AtomicUsize, Ordering}; use inherit_methods_macro::inherit_methods; -use ostd::mm::UntypedMem; +use ostd::{const_assert, mm::UntypedMem}; use super::{ block_ptr::{BidPath, BlockPtrs, Ext2Bid, BID_SIZE, MAX_BLOCK_PTRS}, diff --git a/kernel/src/fs/ext2/prelude.rs b/kernel/src/fs/ext2/prelude.rs index ead2bb334..0c02db033 100644 --- a/kernel/src/fs/ext2/prelude.rs +++ b/kernel/src/fs/ext2/prelude.rs @@ -16,7 +16,6 @@ pub(super) use ostd::{ mm::{Frame, FrameAllocOptions, Segment, USegment, VmIo}, sync::{RwMutex, RwMutexReadGuard, RwMutexWriteGuard}, }; -pub(super) use static_assertions::const_assert; pub(super) use super::utils::{Dirty, IsPowerOf}; pub(super) use crate::{ diff --git a/kernel/src/fs/ext2/super_block.rs b/kernel/src/fs/ext2/super_block.rs index dd520d204..d6859aff2 100644 --- a/kernel/src/fs/ext2/super_block.rs +++ b/kernel/src/fs/ext2/super_block.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: MPL-2.0 +use ostd::const_assert; + use super::{inode::RawInode, prelude::*}; /// The magic number of Ext2. diff --git a/ostd/Cargo.toml b/ostd/Cargo.toml index d4a199203..b4eef027a 100644 --- a/ostd/Cargo.toml +++ b/ostd/Cargo.toml @@ -35,7 +35,6 @@ ostd-test = { version = "0.11.3", path = "libs/ostd-test" } ostd-pod = { git = "https://github.com/asterinas/ostd-pod", rev = "c4644be", version = "0.1.1" } spin = "0.9.4" smallvec = "1.13.2" -static_assertions = "1.1.0" unwinding = { version = "0.2.5", default-features = false, features = ["fde-gnu-eh-frame-hdr", "hide-trace", "panic", "personality", "unwinder"] } volatile = "0.6.1" xarray = { git = "https://github.com/asterinas/xarray", version = "0.1.0" } diff --git a/ostd/src/cpu/set.rs b/ostd/src/cpu/set.rs index f2147369d..441cfca4e 100644 --- a/ostd/src/cpu/set.rs +++ b/ostd/src/cpu/set.rs @@ -5,9 +5,9 @@ use core::sync::atomic::{AtomicU64, Ordering}; use smallvec::SmallVec; -use static_assertions::const_assert_eq; use super::{num_cpus, CpuId}; +use crate::const_assert; /// A subset of all CPUs in the system. #[derive(Clone, Debug, Default)] @@ -159,7 +159,7 @@ pub struct AtomicCpuSet { } type AtomicInnerPart = AtomicU64; -const_assert_eq!(core::mem::size_of::() * 8, BITS_PER_PART); +const_assert!(core::mem::size_of::() * 8 == BITS_PER_PART); impl AtomicCpuSet { /// Creates a new `AtomicCpuSet` with an initial value. diff --git a/ostd/src/lib.rs b/ostd/src/lib.rs index 0aaa70735..0b3b93c1f 100644 --- a/ostd/src/lib.rs +++ b/ostd/src/lib.rs @@ -24,7 +24,6 @@ #![warn(missing_docs)] extern crate alloc; -extern crate static_assertions; pub mod arch; pub mod boot; diff --git a/ostd/src/mm/frame/meta.rs b/ostd/src/mm/frame/meta.rs index 431b2260c..ed6274c62 100644 --- a/ostd/src/mm/frame/meta.rs +++ b/ostd/src/mm/frame/meta.rs @@ -49,11 +49,11 @@ use core::{ use align_ext::AlignExt; use log::info; -use static_assertions::const_assert_eq; use super::{allocator, Segment}; use crate::{ arch::mm::PagingConsts, + const_assert, mm::{ kspace::LINEAR_MAPPING_BASE_VADDR, paddr_to_vaddr, page_size, page_table::boot_pt, CachePolicy, Infallible, Paddr, PageFlags, PageProperty, PrivilegedPageFlags, Vaddr, @@ -124,8 +124,8 @@ pub(super) const REF_COUNT_MAX: u64 = i64::MAX as u64; type FrameMetaVtablePtr = core::ptr::DynMetadata; -const_assert_eq!(PAGE_SIZE % META_SLOT_SIZE, 0); -const_assert_eq!(size_of::(), META_SLOT_SIZE); +const_assert!(PAGE_SIZE % META_SLOT_SIZE == 0); +const_assert!(size_of::() == META_SLOT_SIZE); /// All frame metadata types must implement this trait. /// diff --git a/ostd/src/util.rs b/ostd/src/util.rs index 379dd4b5d..f11ff5140 100644 --- a/ostd/src/util.rs +++ b/ostd/src/util.rs @@ -1,5 +1,20 @@ // SPDX-License-Identifier: MPL-2.0 +/// Asserts that a boolean expression is `true` at compile-time. +/// +/// Rust provides [`const` blocks], which can be used flexibly within methods, but cannot be used +/// directly at the top level. This macro serves as a helper to perform compile-time assertions +/// outside of methods. +/// +/// [`const` blocks]: https://doc.rust-lang.org/reference/expressions/block-expr.html#const-blocks +// +// TODO: Introduce `const_assert_eq!()` once `assert_eq!()` can be used in the `const` context. +#[macro_export] +macro_rules! const_assert { + ($cond:expr $(,)?) => { const _: () = assert!($cond); }; + ($cond:expr, $($arg:tt)+) => { const _: () = assert!($cond, $($arg)*); }; +} + /// A marker trait that represents a type has the same size as `T`. /// /// # Safety