-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Refactoring code 3. Improving examples 4. Improving documentation
- Loading branch information
1 parent
3f7f950
commit 0a0b214
Showing
5 changed files
with
69 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
use cluFullTransmute::transmute_or_panic; | ||
use core::fmt::Display; | ||
|
||
// Implementation of a simple transmutation with a generic parameter inside. | ||
/* | ||
For example, let's write some code with a Drop trait that panics when dropped and | ||
holds some data. We then transmute this data to another similar struct and check | ||
that we have effectively overridden the Drop trait and have a different struct | ||
with some data. | ||
We can also remove the Drop trait altogether or do any number of other things. | ||
*/ | ||
|
||
/// Struct to panic when dropped | ||
#[derive(Debug)] | ||
#[repr(transparent)] | ||
struct A<T> { | ||
#[allow(dead_code)] | ||
data: T, | ||
} | ||
struct PanicWhenDrop<T>(T); | ||
|
||
impl<T> Drop for A<T> { | ||
impl<T> Drop for PanicWhenDrop<T> { | ||
fn drop(&mut self) { | ||
panic!("Invalid beh"); | ||
panic!("panic, discovered `drop(PanicWhenDrop);`"); | ||
} | ||
} | ||
|
||
/// Struct to print value when dropped | ||
#[derive(Debug)] | ||
#[repr(transparent)] | ||
struct B<T> | ||
struct PrintlnWhenDrop<T: Display>(T) | ||
where | ||
T: Display, | ||
{ | ||
data: T, | ||
} | ||
T: Display; | ||
|
||
impl<T> Drop for B<T> | ||
impl<T> Drop for PrintlnWhenDrop<T> | ||
where | ||
T: Display, | ||
{ | ||
fn drop(&mut self) { | ||
println!("{}", self.data); | ||
println!("println: {}", self.0); | ||
} | ||
} | ||
|
||
fn main() { | ||
let a: A<u16> = A { | ||
// original and panic when falling | ||
data: 1024, | ||
}; | ||
println!("in: {:?}", a); | ||
let a: PanicWhenDrop<u16> = PanicWhenDrop(1024); | ||
println!("in a: {:?}", a); | ||
|
||
let b: B<u16> = unsafe { transmute_or_panic(a) }; | ||
println!("out: {:?}", b); | ||
let b: PrintlnWhenDrop<u16> = unsafe { transmute_or_panic(a as PanicWhenDrop<u16>) }; | ||
println!("out b: {:?}", b); | ||
|
||
drop(b); // <--- println! | ||
drop(b); // <--- drop, PrintlnWhenDrop! | ||
} |
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