1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
use mokuya::components::prelude::*;
pub fn generate_parse_mutex(input: &DeriveInput) -> TokenStream {
let struct_name = get_struct_name(input);
quote! {
pub struct ParseMutex(std::sync::Mutex<#struct_name>);
impl ParseMutex {
/// Consumes self and returns the inner `Mutex` containing the struct.
pub fn get(self) -> std::sync::Mutex<#struct_name> {
self.0
}
/// Consumes self and returns an `Arc` wrapping the `Mutex` for shared thread-safe ownership.
pub fn arc(self) -> std::sync::Arc<std::sync::Mutex<#struct_name>> {
std::sync::Arc::new(self.0)
}
/// Consumes self and returns a boxed `Mutex`.
pub fn boxed(self) -> Box<std::sync::Mutex<#struct_name>> {
Box::new(self.0)
}
/// Consumes self and returns a `RefCell` wrapping the `Mutex`.
pub fn ref_cell(self) -> std::cell::RefCell<std::sync::Mutex<#struct_name>> {
std::cell::RefCell::new(self.0)
}
/// Consumes self and returns an `UnsafeCell` wrapping the `Mutex`.
pub fn unsafe_cell(self) -> std::cell::UnsafeCell<std::sync::Mutex<#struct_name>> {
std::cell::UnsafeCell::new(self.0)
}
/// Consumes self and returns a `OnceCell` wrapping the `Mutex`.
/// The `OnceCell` is initialized with the `Mutex` value.
pub fn once_cell(self) -> std::cell::OnceCell<std::sync::Mutex<#struct_name>> {
let cell = std::cell::OnceCell::new();
cell.set(self.0).ok();
cell
}
}
}
}