Skip to content

Instantly share code, notes, and snippets.

@WaffleLapkin
Last active April 27, 2022 08:05
Show Gist options
  • Save WaffleLapkin/3f7ce31a97e6bded631c316a56838c15 to your computer and use it in GitHub Desktop.
Save WaffleLapkin/3f7ce31a97e6bded631c316a56838c15 to your computer and use it in GitHub Desktop.
// 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>>();
}
// 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