create shared utility fn transmute_data_ptr

This commit is contained in:
Niko Matsakis 2024-08-12 07:59:08 +03:00
parent 188f759555
commit 1fbca6d756
2 changed files with 13 additions and 10 deletions

View file

@ -1,4 +1,4 @@
use crate::Database;
use crate::{zalsa::transmute_data_ptr, Database};
use append_only_vec::AppendOnlyVec;
use std::{
any::{Any, TypeId},
@ -173,14 +173,6 @@ impl std::fmt::Debug for ViewCaster {
}
}
/// Given a wide pointer `T`, extracts the data pointer (typed as `()`).
/// This is safe because `()` gives no access to any data and has no validity requirements in particular.
unsafe fn data_ptr<T: ?Sized, U>(t: &T) -> &U {
let t: *const T = t;
let u: *const U = t as *const U;
unsafe { &*u }
}
impl<Db, DbView> CastTo<DbView> for fn(&Db) -> &DbView
where
Db: Database,
@ -194,7 +186,7 @@ where
//
// Caller guarantees that the input is of type `Db`
// (we test it in the debug-assertion above).
let db = unsafe { data_ptr::<dyn Database, Db>(db) };
let db = unsafe { transmute_data_ptr::<dyn Database, Db>(db) };
(*self)(db)
}

View file

@ -316,3 +316,14 @@ where
}
}
}
/// Given a wide pointer `T`, extracts the data pointer (typed as `U`).
///
/// # Safety requirement
///
/// `U` must be correct type for the data pointer.
pub(crate) unsafe fn transmute_data_ptr<T: ?Sized, U>(t: &T) -> &U {
let t: *const T = t;
let u: *const U = t as *const U;
unsafe { &*u }
}