Last active
April 27, 2022 08:05
-
-
Save WaffleLapkin/3f7ce31a97e6bded631c316a56838c15 to your computer and use it in GitHub Desktop.
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
// pseudocode | |
pub struct ThinSlice<T> { | |
len: usize, | |
slice: [T], | |
} | |
impl Pointee for ThinSlice<T> { | |
type Metadata = ThinSliceMetadata<T>; | |
// ??????? | |
unsafe fn size_of(ptr: *const Self) -> usize { | |
unsafe { (*ptr).len * size_of::<T>() } | |
} | |
} | |
#[derive(all_the_stuff)] | |
pub struct ThinSliceMetadata<T>(PhantomData<T>); | |
pub struct ThinArray<T, const N: usize> { | |
len: usize, // always = N to support unsizing | |
array: [T; N], | |
} | |
// ??? | |
impl<T, const N: usize> Unsize<ThinSlice<T>> for ThinArray<T, N> {} | |
fn main() { | |
// Noop, ptr doesn't need any adjustments | |
let _: *const ThinSlice<u8> = ptr::null::<ThinArray<u8, 17>>(); | |
} |
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
// pseudocode | |
pub struct ThinAny { | |
meta: DynMetadata<dyn Any>, | |
obj: dyn Any, | |
} | |
impl Pointee for ThinAny { | |
type Metadata = ThinAnyMetadata; | |
// ??????? | |
unsafe fn align_of(ptr: *const Self) -> usize { | |
unsafe { (*ptr).meta.align_of() } | |
} | |
// ??????? | |
unsafe fn size_of(ptr: *const Self) -> usize { | |
unsafe { (*ptr).meta.size_of() } | |
} | |
} | |
#[derive(all_the_stuff)] | |
#[non_exhaustive] | |
pub struct ThinAnyMetadata{}; | |
pub struct ThinObj<T> { | |
meta: DynMetadata<dyn Any>, // to support unsizing | |
obj: T, | |
} | |
// ??? | |
impl<T> Unsize<ThinAny> for ThinObj<T> {} | |
fn main() { | |
// Noop, ptr doesn't need any adjustments | |
let _: *const ThinAny = ptr::null::<ThinObj<u8>>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment