Skip to content

Commit

Permalink
Refactor Capable::vtable to return Option<NonNull<()>>
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebear94 committed May 25, 2024
1 parent ea4c64a commit 55ba532
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/typst-macros/src/elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ fn create_capable_impl(element: &Elem) -> TokenStream {

quote! {
unsafe impl #foundations::Capable for #ident {
fn vtable(capability: ::std::any::TypeId) -> ::std::option::Option<*const ()> {
fn vtable(capability: ::std::any::TypeId) -> ::std::option::Option<::std::ptr::NonNull<()>> {
let dangling = ::std::ptr::NonNull::<#foundations::Packed<#ident>>::dangling().as_ptr();
#(#checks)*
None
Expand Down
7 changes: 5 additions & 2 deletions crates/typst-utils/src/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use std::alloc::Layout;
use std::mem;
use std::ptr::NonNull;

/// Create a fat pointer from a data address and a vtable address.
///
Expand Down Expand Up @@ -39,9 +40,11 @@ pub unsafe fn from_raw_parts_mut<T: ?Sized>(data: *mut (), vtable: *const ()) ->
/// # Safety
/// Must only be called when `T` is a `dyn Trait`.
#[track_caller]
pub unsafe fn vtable<T: ?Sized>(ptr: *const T) -> *const () {
pub unsafe fn vtable<T: ?Sized>(ptr: *const T) -> NonNull<()> {
debug_assert_eq!(Layout::new::<*const T>(), Layout::new::<FatPointer>());
mem::transmute_copy::<*const T, FatPointer>(&ptr).vtable
NonNull::new_unchecked(
mem::transmute_copy::<*const T, FatPointer>(&ptr).vtable as *mut (),
)
}

/// The memory representation of a trait object pointer.
Expand Down
4 changes: 2 additions & 2 deletions crates/typst/src/foundations/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Content {
// use a `*const Content` pointer.
let vtable = self.elem().vtable()(TypeId::of::<C>())?;
let data = self as *const Content as *const ();
Some(unsafe { &*fat::from_raw_parts(data, vtable) })
Some(unsafe { &*fat::from_raw_parts(data, vtable.as_ptr()) })
}

/// Cast to a mutable trait object if the contained element has the given
Expand All @@ -319,7 +319,7 @@ impl Content {
// mutable access is required.
let vtable = self.elem().vtable()(TypeId::of::<C>())?;
let data = self as *mut Content as *mut ();
Some(unsafe { &mut *fat::from_raw_parts_mut(data, vtable) })
Some(unsafe { &mut *fat::from_raw_parts_mut(data, vtable.as_ptr()) })
}

/// Whether the content is an empty sequence.
Expand Down
7 changes: 4 additions & 3 deletions crates/typst/src/foundations/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::any::TypeId;
use std::cmp::Ordering;
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::ptr::NonNull;

use ecow::EcoString;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -81,7 +82,7 @@ impl Element {
}

/// The VTable for capabilities dispatch.
pub fn vtable(self) -> fn(of: TypeId) -> Option<*const ()> {
pub fn vtable(self) -> fn(of: TypeId) -> Option<NonNull<()>> {
self.0.vtable
}

Expand Down Expand Up @@ -208,7 +209,7 @@ pub trait NativeElement:
/// `TypeId::of::<dyn C>()`.
pub unsafe trait Capable {
/// Get the pointer to the vtable for the given capability / trait.
fn vtable(capability: TypeId) -> Option<*const ()>;
fn vtable(capability: TypeId) -> Option<NonNull<()>>;
}

/// Defines how fields of an element are accessed.
Expand Down Expand Up @@ -267,7 +268,7 @@ pub struct NativeElementData {
pub keywords: &'static [&'static str],
pub construct: fn(&mut Engine, &mut Args) -> SourceResult<Content>,
pub set: fn(&mut Engine, &mut Args) -> SourceResult<Styles>,
pub vtable: fn(capability: TypeId) -> Option<*const ()>,
pub vtable: fn(capability: TypeId) -> Option<NonNull<()>>,
pub field_id: fn(name: &str) -> Option<u8>,
pub field_name: fn(u8) -> Option<&'static str>,
pub field_from_styles: fn(u8, StyleChain) -> Option<Value>,
Expand Down

0 comments on commit 55ba532

Please sign in to comment.