-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Closes #471] Add const_assert_generics
module
#489
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ use core::{ | |
use static_assertions::const_assert; | ||
|
||
use crate::arch::addr::{PAddr, PGSIZE}; | ||
use crate::util::const_assert_generics::{Assert2, True}; | ||
|
||
// `RawPage` must be aligned with PGSIZE. | ||
const_assert!(PGSIZE == 4096); | ||
|
@@ -75,16 +76,10 @@ impl Page { | |
} | ||
} | ||
|
||
pub fn as_uninit_mut<T>(&mut self) -> &mut MaybeUninit<T> { | ||
// TODO(https://github.com/kaist-cp/rv6/issues/471): | ||
// Use const_assert! (or equivalent) instead. Currently, use of T inside const_assert! | ||
// incurs a compile error: "can't use generic parameters from outer function". | ||
// Also, there's a workaround using feature(const_generics) and | ||
// feature(const_evaluatable_checked). However, using them makes the compiler panic. | ||
// When the compiler becomes updated, we will fix the following lines to use static checks. | ||
assert!(mem::size_of::<T>() <= PGSIZE); | ||
assert_eq!(PGSIZE % mem::align_of::<T>(), 0); | ||
|
||
pub fn as_uninit_mut<T>(&mut self) -> &mut MaybeUninit<T> | ||
where | ||
Assert2<{ mem::size_of::<T>() <= PGSIZE }, { PGSIZE % mem::align_of::<T>() == 0 }>: True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 참고: 이 줄을 |
||
{ | ||
// SAFETY: self.inner is an array of length PGSIZE aligned with PGSIZE bytes. | ||
// The above assertions show that it can contain a value of T. As it contains arbitrary | ||
// data, we cannot treat it as &mut T. Instead, we use &mut MaybeUninit<T>. It's ok because | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! Types that let you compile-time assert in `where` clauses, | ||
//! especially about the input generic parameters. | ||
//! | ||
//! # Example | ||
//! ```rust,no_run | ||
//! # use core::mem; | ||
//! #![feature(const_generics)] | ||
//! #![feature(const_evaluatable_checked)] | ||
//! | ||
//! unsafe fn transmute<T, U>(t: T) -> U | ||
//! where | ||
//! Assert2< | ||
//! { mem::size_of::<T>() == mem::size_of::<U>() }, | ||
//! { mem::align_of::<T>() == mem::align_of::<U>() }, | ||
//! >: True | ||
//! { | ||
//! /* Omitted */ | ||
//! } | ||
//! ``` | ||
|
||
pub struct Assert<const EXPR: bool>; | ||
pub struct Assert2<const EXPR: bool, const EXPR2: bool>; | ||
pub struct Assert3<const EXPR: bool, const EXPR2: bool, const EXPR3: bool>; | ||
|
||
pub trait True {} | ||
impl True for Assert<true> {} | ||
impl True for Assert2<true, true> {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EDIT: 다만, 이런 방식으로 사용하자는 discussion들이 있기는 합니다. |
||
impl True for Assert3<true, true, true> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아직
const_evaluatable_check
feature가 unstable해서, 이걸 끄지 않고 debug mode로 컴파일하면 ICE가 발생합니다. (rust-lang/rust#77708)