-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
neon::instance::Global
API for storing instance-global data.
- Loading branch information
Showing
7 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::any::Any; | ||
use std::marker::PhantomData; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
use once_cell::sync::OnceCell; | ||
|
||
use crate::context::Context; | ||
use crate::lifecycle::InstanceData; | ||
|
||
static COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
||
fn next_id() -> usize { | ||
COUNTER.fetch_add(1, Ordering::SeqCst) | ||
} | ||
|
||
/// A cell that can be used to allocate data that is global to an instance | ||
/// of a Neon addon. | ||
pub struct Global<T> { | ||
_type: PhantomData<T>, | ||
id: OnceCell<usize>, | ||
} | ||
|
||
impl<T> Global<T> { | ||
pub const fn new() -> Self { | ||
Self { | ||
_type: PhantomData, | ||
id: OnceCell::new(), | ||
} | ||
} | ||
|
||
fn id(&self) -> usize { | ||
*self.id.get_or_init(next_id) | ||
} | ||
} | ||
|
||
impl<T: Any + Send> Global<T> { | ||
pub fn borrow<'a, 'b, C>(&self, cx: &'b mut C) -> Option<&'b T> | ||
where | ||
C: Context<'a>, | ||
{ | ||
InstanceData::globals(cx)[self.id()] | ||
.as_ref() | ||
.map(|boxed| boxed.downcast_ref().unwrap()) | ||
} | ||
|
||
pub fn borrow_mut<'a, 'b, C>(&self, cx: &'b mut C) -> Option<&'b mut T> | ||
where | ||
C: Context<'a>, | ||
{ | ||
InstanceData::globals(cx)[self.id()] | ||
.as_mut() | ||
.map(|boxed| boxed.downcast_mut().unwrap()) | ||
} | ||
|
||
pub fn set<'a, 'b, C>(&self, cx: &'b mut C, v: T) | ||
where | ||
C: Context<'a>, | ||
{ | ||
InstanceData::globals(cx)[self.id()] = Some(Box::new(v)); | ||
} | ||
} | ||
|
||
impl<T: Any + Send + Clone> Global<T> { | ||
pub fn get<'a, C>(&self, cx: &mut C) -> Option<T> | ||
where | ||
C: Context<'a>, | ||
{ | ||
InstanceData::globals(cx)[self.id()] | ||
.as_ref() | ||
.map(|boxed| boxed.downcast_ref::<T>().unwrap().clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters