mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-10-23 04:46:35 +00:00
create shared utility fn transmute_data_ptr
This commit is contained in:
parent
188f759555
commit
1fbca6d756
2 changed files with 13 additions and 10 deletions
12
src/views.rs
12
src/views.rs
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
11
src/zalsa.rs
11
src/zalsa.rs
|
@ -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 }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue