mirror of https://github.com/boa-dev/boa.git
Browse Source
This allows `thread_local` contexts to have owned `HostHooks` and `JobQueues`. It changes the following: - Creates a new `MaybeShared` struct that can hold either a reference or an `Rc`. - Changes the `job_queue` and `host_hooks` parameters of `Context` to use `MaybeShared`. This PR also allows us to make `SimpleJobQueue` the default promise runner, which I think it's pretty cool :) cc @lastmjspull/2814/head
José Julián Espina
2 years ago
14 changed files with 147 additions and 119 deletions
@ -0,0 +1,42 @@
|
||||
use std::{ops::Deref, rc::Rc}; |
||||
|
||||
/// A [`Cow`][std::borrow::Cow]-like pointer where the `Owned` variant is an [`Rc`].
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] |
||||
pub enum MaybeShared<'a, T: ?Sized> { |
||||
/// Borrowed data.
|
||||
Borrowed(&'a T), |
||||
/// `Rc` shared data.
|
||||
Shared(Rc<T>), |
||||
} |
||||
|
||||
impl<T: ?Sized> Clone for MaybeShared<'_, T> { |
||||
fn clone(&self) -> Self { |
||||
match self { |
||||
Self::Borrowed(b) => Self::Borrowed(b), |
||||
Self::Shared(sh) => Self::Shared(sh.clone()), |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl<T: ?Sized> Deref for MaybeShared<'_, T> { |
||||
type Target = T; |
||||
|
||||
fn deref(&self) -> &Self::Target { |
||||
match self { |
||||
MaybeShared::Borrowed(b) => b, |
||||
MaybeShared::Shared(sh) => sh, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl<'a, T: ?Sized> From<&'a T> for MaybeShared<'a, T> { |
||||
fn from(value: &'a T) -> Self { |
||||
Self::Borrowed(value) |
||||
} |
||||
} |
||||
|
||||
impl<T: ?Sized> From<Rc<T>> for MaybeShared<'static, T> { |
||||
fn from(value: Rc<T>) -> Self { |
||||
Self::Shared(value) |
||||
} |
||||
} |
Loading…
Reference in new issue