-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
const_mut_refs: allow mutable refs to statics
- Loading branch information
Showing
8 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// check-pass | ||
//! This is the reduced version of the "Linux kernel vtable" use-case. | ||
#![feature(const_mut_refs, const_refs_to_static)] | ||
use std::ptr::addr_of_mut; | ||
|
||
#[repr(C)] | ||
struct ThisModule(i32); | ||
|
||
trait Module { | ||
const THIS_MODULE_PTR: *mut ThisModule; | ||
} | ||
|
||
struct MyModule; | ||
|
||
// Generated by a macro. | ||
extern "C" { | ||
static mut THIS_MODULE: ThisModule; | ||
} | ||
|
||
// Generated by a macro. | ||
impl Module for MyModule { | ||
const THIS_MODULE_PTR: *mut ThisModule = unsafe { addr_of_mut!(THIS_MODULE) }; | ||
} | ||
|
||
struct Vtable { | ||
module: *mut ThisModule, | ||
foo_fn: fn(*mut ()) -> i32, | ||
} | ||
|
||
trait Foo { | ||
type Mod: Module; | ||
|
||
fn foo(&mut self) -> i32; | ||
} | ||
|
||
fn generate_vtable<T: Foo>() -> &'static Vtable { | ||
&Vtable { | ||
module: T::Mod::THIS_MODULE_PTR, | ||
foo_fn: |ptr| unsafe { &mut *ptr.cast::<T>() }.foo(), | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// run-pass | ||
#![feature(const_mut_refs)] | ||
#![feature(sync_unsafe_cell)] | ||
|
||
use std::cell::SyncUnsafeCell; | ||
use std::ptr; | ||
|
||
#[repr(C)] | ||
struct SyncPtr { | ||
foo: *mut u32, | ||
} | ||
unsafe impl Sync for SyncPtr {} | ||
|
||
static mut STATIC: u32 = 42; | ||
|
||
static INTERIOR_MUTABLE_STATIC: SyncUnsafeCell<u32> = SyncUnsafeCell::new(42); | ||
|
||
// A static that mutably points to STATIC. | ||
static PTR: SyncPtr = SyncPtr { | ||
foo: unsafe { ptr::addr_of_mut!(STATIC) }, | ||
}; | ||
static INTERIOR_MUTABLE_PTR: SyncPtr = SyncPtr { | ||
foo: ptr::addr_of!(INTERIOR_MUTABLE_STATIC) as *mut u32, | ||
}; | ||
|
||
fn main() { | ||
let ptr = PTR.foo; | ||
unsafe { | ||
assert_eq!(*ptr, 42); | ||
*ptr = 0; | ||
assert_eq!(*PTR.foo, 0); | ||
} | ||
|
||
let ptr = INTERIOR_MUTABLE_PTR.foo; | ||
unsafe { | ||
assert_eq!(*ptr, 42); | ||
*ptr = 0; | ||
assert_eq!(*INTERIOR_MUTABLE_PTR.foo, 0); | ||
} | ||
} |