mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 22:54:36 +00:00
vtable: make sure one cannot call the function with the wrong type
This commit is contained in:
parent
0a973f65d5
commit
903fc513de
2 changed files with 18 additions and 28 deletions
|
@ -221,6 +221,11 @@ pub fn vtable(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
forward_code = Some(quote!(#forward_code #arg_name,));
|
forward_code = Some(quote!(#forward_code #arg_name,));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if has_self {
|
||||||
|
// Add unsafe: The function are not safe to call unless the self parameter is of the correct type
|
||||||
|
f.unsafety = Some(Default::default());
|
||||||
|
}
|
||||||
|
|
||||||
// Add extern "C" if it isn't there
|
// Add extern "C" if it isn't there
|
||||||
if let Some(a) = &f.abi {
|
if let Some(a) = &f.abi {
|
||||||
if !a.name.as_ref().map(|s| s.value() == "C").unwrap_or(false) {
|
if !a.name.as_ref().map(|s| s.value() == "C").unwrap_or(false) {
|
||||||
|
@ -465,4 +470,3 @@ pub fn vtable(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
// println!("{}", result);
|
// println!("{}", result);
|
||||||
result.into()
|
result.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
use core::marker::PhantomData;
|
||||||
use core::ops::{Deref, DerefMut, Drop};
|
use core::ops::{Deref, DerefMut, Drop};
|
||||||
use core::ptr::NonNull;
|
use core::ptr::NonNull;
|
||||||
use core::marker::PhantomData;
|
|
||||||
pub use vtable_macro::*;
|
pub use vtable_macro::*;
|
||||||
|
|
||||||
pub unsafe trait VTableMeta {
|
pub unsafe trait VTableMeta {
|
||||||
|
@ -13,7 +13,6 @@ pub unsafe trait VTableMeta {
|
||||||
/// NOTE: the size must be 2*size_of<usize>
|
/// NOTE: the size must be 2*size_of<usize>
|
||||||
type TraitObject: Copy;
|
type TraitObject: Copy;
|
||||||
|
|
||||||
|
|
||||||
/// That maps from the tait object from the trait iteself
|
/// That maps from the tait object from the trait iteself
|
||||||
/// (In other word, return 'to' since 'to' implements trait,
|
/// (In other word, return 'to' since 'to' implements trait,
|
||||||
/// but we can't represent that in rust right now, hence these helper)
|
/// but we can't represent that in rust right now, hence these helper)
|
||||||
|
@ -31,8 +30,6 @@ pub unsafe trait VTableMeta {
|
||||||
|
|
||||||
/// return a reference to the vtable
|
/// return a reference to the vtable
|
||||||
unsafe fn get_vtable(from: &Self::TraitObject) -> &Self::VTable;
|
unsafe fn get_vtable(from: &Self::TraitObject) -> &Self::VTable;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait VTableMetaDrop: VTableMeta {
|
pub trait VTableMetaDrop: VTableMeta {
|
||||||
|
@ -88,27 +85,13 @@ impl<T: ?Sized + VTableMetaDrop> VBox<T> {
|
||||||
pub fn get_vtable(&self) -> &T::VTable {
|
pub fn get_vtable(&self) -> &T::VTable {
|
||||||
unsafe { T::get_vtable(&self.inner) }
|
unsafe { T::get_vtable(&self.inner) }
|
||||||
}
|
}
|
||||||
|
pub fn borrow<'b>(&'b self) -> VRef<'b, T> {
|
||||||
|
unsafe { VRef::from_inner(self.inner) }
|
||||||
}
|
}
|
||||||
|
pub fn borrow_mut<'b>(&'b mut self) -> VRefMut<'b, T> {
|
||||||
/*
|
unsafe { VRefMut::from_inner(self.inner) }
|
||||||
impl<T: ?Sized + VTableMeta> VBox<T> {
|
|
||||||
/// Construct the box from raw pointer of a vtable and a corresponding pointer
|
|
||||||
pub unsafe fn from_inner(
|
|
||||||
vtable: core::ptr::NonNull<#vtable_name>,
|
|
||||||
ptr: core::ptr::NonNull<#impl_name>,
|
|
||||||
) -> Self {
|
|
||||||
Self{inner: #to_name{vtable, ptr}}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*pub fn vtable(&self) -> & #vtable_name {
|
|
||||||
unsafe { self.inner.vtable.as_ref() }
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/* pub fn get_ptr(&self) -> *mut #impl_name {
|
|
||||||
self.inner.ptr.get_ptr()
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
pub struct VRef<'a, T: ?Sized + VTableMeta> {
|
pub struct VRef<'a, T: ?Sized + VTableMeta> {
|
||||||
inner: T::TraitObject,
|
inner: T::TraitObject,
|
||||||
|
@ -196,3 +179,6 @@ impl<'a, T: ?Sized + VTableMeta> VRefMut<'a, T> {
|
||||||
unsafe { T::get_vtable(&self.inner) }
|
unsafe { T::get_vtable(&self.inner) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(doctest)]
|
||||||
|
mod compile_fail_tests;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue