mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Implement MutNullableJS::or_init.
This will make it easier to implement lazy getters.
This commit is contained in:
parent
9416e9b11a
commit
d26283c065
1 changed files with 11 additions and 0 deletions
|
@ -248,6 +248,17 @@ impl<T: Reflectable> MutNullableJS<T> {
|
||||||
pub unsafe fn get_inner(&self) -> Option<JS<T>> {
|
pub unsafe fn get_inner(&self) -> Option<JS<T>> {
|
||||||
self.ptr.get()
|
self.ptr.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn or_init(&self, cb: || -> Temporary<T>) -> Temporary<T> {
|
||||||
|
match self.get() {
|
||||||
|
Some(inner) => inner,
|
||||||
|
None => {
|
||||||
|
let inner = cb();
|
||||||
|
self.assign(Some(inner));
|
||||||
|
inner
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Reflectable> JS<T> {
|
impl<T: Reflectable> JS<T> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue