rquickjs_core/
markers.rs

1//! Utility types and traits.
2
3use std::marker::PhantomData;
4
5// Super nice trick taken from the rlua library.
6// Can be used to pin a lifetime so that all functions which use
7// that lifetime can only us that single lifetime and not one which
8// is variant over that lifetime
9/// A marker struct which marks a lifetime as invariant.
10#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug, Default)]
11pub struct Invariant<'inv>(PhantomData<&'inv mut &'inv fn(&'inv ()) -> &'inv ()>);
12
13impl<'inv> Invariant<'inv> {
14    pub fn new() -> Self {
15        Invariant(PhantomData)
16    }
17
18    pub fn new_ref<T>(_v: &'inv T) -> Self {
19        Invariant(PhantomData)
20    }
21}
22
23/// The marker trait which requires [`Send`] when `"parallel"` feature is used
24#[cfg(not(feature = "parallel"))]
25pub trait ParallelSend {}
26
27#[cfg(feature = "parallel")]
28pub trait ParallelSend: Send {}
29
30#[cfg(not(feature = "parallel"))]
31impl<T> ParallelSend for T {}
32
33#[cfg(feature = "parallel")]
34impl<T: Send> ParallelSend for T {}