@@ -45,7 +45,7 @@ pub unsafe trait ProvidesStaticType<'a> {
45
45
/// Note `ProvidesStaticType` and `AnyLifetime` cannot be the same type,
46
46
/// because `AnyLifetime` need to be object safe,
47
47
/// and `ProvidesStaticType` has type member.
48
- unsafe impl < ' a , T : ProvidesStaticType < ' a > + ' a + ?Sized > AnyLifetime < ' a > for T {
48
+ impl < ' a , T : ProvidesStaticType < ' a > + ' a + ?Sized > AnyLifetime < ' a > for T {
49
49
fn static_type_id ( ) -> TypeId
50
50
where
51
51
Self : Sized ,
@@ -58,15 +58,14 @@ unsafe impl<'a, T: ProvidesStaticType<'a> + 'a + ?Sized> AnyLifetime<'a> for T {
58
58
}
59
59
}
60
60
61
- /// Like [`Any`](std::any::Any), but while [`Any`](std::any::Any) requires `'static`,
62
- /// this version allows a lifetime parameter.
61
+ /// Like [`Any`](std::any::Any), but while [`Any`](std::any::Any) requires `'static`, this version
62
+ /// allows a lifetime parameter.
63
63
///
64
- /// Code using this trait is _unsafe_ if your implementation of the inner
65
- /// methods do not meet the invariants listed. Therefore, it is recommended you
66
- /// use one of the helper macros.
64
+ /// Code using this trait is _unsafe_ if your implementation of the inner methods do not meet the
65
+ /// invariants listed. Therefore, it is recommended you use one of the helper macros.
67
66
///
68
- /// If your data type is of the form `Foo` or `Foo<'v>` you can derive
69
- /// `AnyLifetime` :
67
+ /// You cannot implement this trait directly. You should instead implement `ProvidesStaticType`,
68
+ /// usually via the derive macro :
70
69
///
71
70
/// ```
72
71
/// use starlark::any::ProvidesStaticType;
@@ -76,8 +75,8 @@ unsafe impl<'a, T: ProvidesStaticType<'a> + 'a + ?Sized> AnyLifetime<'a> for T {
76
75
/// struct Foo2<'a>(&'a ());
77
76
/// ```
78
77
///
79
- /// For more complicated context or constraints, you can implement `ProvidesStaticType`
80
- /// directly.
78
+ /// If your data type is not of the form `Foo` or `Foo<'v>` you may need to implement
79
+ /// `ProvidesStaticType` directly.
81
80
///
82
81
/// ```
83
82
/// use starlark::any::ProvidesStaticType;
@@ -94,7 +93,7 @@ unsafe impl<'a, T: ProvidesStaticType<'a> + 'a + ?Sized> AnyLifetime<'a> for T {
94
93
/// }
95
94
/// # }
96
95
/// ```
97
- pub unsafe trait AnyLifetime < ' a > : ' a {
96
+ pub trait AnyLifetime < ' a > : seal :: ProvidesStaticTypeSealed < ' a > + ' a {
98
97
/// Must return the `TypeId` of `Self` but where the lifetimes are changed
99
98
/// to `'static`. Must be consistent with `static_type_of`.
100
99
fn static_type_id ( ) -> TypeId
@@ -108,6 +107,12 @@ pub unsafe trait AnyLifetime<'a>: 'a {
108
107
// Required so we can have a `dyn AnyLifetime`.
109
108
}
110
109
110
+ mod seal {
111
+ /// A bound required by `AnyLifetime<'a>` for sealing it
112
+ pub trait ProvidesStaticTypeSealed < ' a > { }
113
+ impl < ' a , T : super :: ProvidesStaticType < ' a > + ?Sized > ProvidesStaticTypeSealed < ' a > for T { }
114
+ }
115
+
111
116
impl < ' a > dyn AnyLifetime < ' a > {
112
117
/// Is the value of type `T`.
113
118
pub fn is < T : AnyLifetime < ' a > > ( & self ) -> bool {
0 commit comments