@@ -20,7 +20,7 @@ use crate::{
20
20
} ;
21
21
use indexmap:: { map:: Entry , IndexMap } ;
22
22
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 } ;
24
24
25
25
/// type(object_or_name, bases, dict)
26
26
/// type(object) -> the object's type
@@ -41,7 +41,7 @@ pub struct HeapTypeExt {
41
41
pub number_methods : PyNumberMethods ,
42
42
}
43
43
44
- pub struct PointerSlot < T > ( NonNull < T > ) ;
44
+ pub struct PointerSlot < T : ' static > ( & ' static T ) ;
45
45
46
46
impl < T > Clone for PointerSlot < T > {
47
47
fn clone ( & self ) -> Self {
@@ -53,24 +53,28 @@ impl<T> Copy for PointerSlot<T> {}
53
53
54
54
impl < T > From < & ' static T > for PointerSlot < T > {
55
55
fn from ( x : & ' static T ) -> Self {
56
- Self ( NonNull :: from ( x ) )
56
+ Self ( x )
57
57
}
58
58
}
59
59
60
60
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
63
63
}
64
64
}
65
65
66
66
impl < T > PointerSlot < T > {
67
+ /// # Safety
68
+ /// typ must live long enough
67
69
pub unsafe fn from_heaptype < F > ( typ : & PyType , f : F ) -> Option < Self >
68
70
where
69
- F : FnOnce ( & HeapTypeExt ) -> & T ,
71
+ F : FnOnce ( & ' static HeapTypeExt ) -> & ' static T ,
70
72
{
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
+ } )
74
78
}
75
79
}
76
80
0 commit comments