salsa/salsa-2022-tests/tests/create-empty-database.rs
DropDemBits 5b8412656c
Initialize jars in-place
Requires unsafe, since Rust currently doesn't have built-in
support for guaranteed in-place initialization. Unfortunately,
this unsafety propagates to making `Jar` unsafe to implement
in order to guarantee that the jar is initialized, but since
the preferred way to implement it is via `salsa::jar`, this
won't impact most users.
2023-06-14 01:09:58 -04:00

35 lines
790 B
Rust

//! Tests that we can create a database of only 0-size jars without invoking UB
use salsa::storage::HasJars;
#[salsa::jar(db = Db)]
struct Jar();
trait Db: salsa::DbWithJar<Jar> {}
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
impl salsa::Database for Database {}
impl Db for Database {}
#[test]
fn execute() {
let db = Database::default();
let jars = db.storage.jars().0;
ensure_init(jars);
}
fn ensure_init(place: *const <Database as HasJars>::Jars) {
use std::mem::forget;
use std::ptr::addr_of;
// SAFETY: Intentionally tries to access potentially uninitialized memory,
// so that miri can catch if we accidentally forget to initialize the memory.
forget(unsafe { addr_of!((*place).0).read() });
}