Skip to content

Commit 3884481

Browse files
committed
PointerSlot uses &'static
1 parent e8fe8ea commit 3884481

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

vm/src/builtins/type.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
};
2121
use indexmap::{map::Entry, IndexMap};
2222
use itertools::Itertools;
23-
use std::{borrow::Borrow, collections::HashSet, fmt, ops::Deref, pin::Pin, ptr::NonNull};
23+
use std::{borrow::Borrow, collections::HashSet, fmt, ops::Deref, pin::Pin};
2424

2525
/// type(object_or_name, bases, dict)
2626
/// type(object) -> the object's type
@@ -41,7 +41,7 @@ pub struct HeapTypeExt {
4141
pub number_methods: PyNumberMethods,
4242
}
4343

44-
pub struct PointerSlot<T>(NonNull<T>);
44+
pub struct PointerSlot<T: 'static>(&'static T);
4545

4646
impl<T> Clone for PointerSlot<T> {
4747
fn clone(&self) -> Self {
@@ -53,24 +53,28 @@ impl<T> Copy for PointerSlot<T> {}
5353

5454
impl<T> From<&'static T> for PointerSlot<T> {
5555
fn from(x: &'static T) -> Self {
56-
Self(NonNull::from(x))
56+
Self(x)
5757
}
5858
}
5959

6060
impl<T> AsRef<T> for PointerSlot<T> {
61-
fn as_ref(&self) -> &T {
62-
unsafe { self.0.as_ref() }
61+
fn as_ref(&self) -> &'static T {
62+
self.0
6363
}
6464
}
6565

6666
impl<T> PointerSlot<T> {
67+
/// # Safety
68+
/// typ must live long enough
6769
pub unsafe fn from_heaptype<F>(typ: &PyType, f: F) -> Option<Self>
6870
where
69-
F: FnOnce(&HeapTypeExt) -> &T,
71+
F: FnOnce(&'static HeapTypeExt) -> &'static T,
7072
{
71-
typ.heaptype_ext
72-
.as_ref()
73-
.map(|ext| Self(NonNull::from(f(ext))))
73+
typ.heaptype_ext.as_ref().map(|ext| {
74+
let ext: &HeapTypeExt = &ext.as_ref();
75+
let ext: &'static _ = &*(ext as *const _); // lease lifetime by trusting `typ` live long enough
76+
Self(f(ext))
77+
})
7478
}
7579
}
7680

0 commit comments

Comments
 (0)