Skip to content

Commit

Permalink
derive: no need to use Arcs at all
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 1, 2024
1 parent 74bd0cb commit 538c0e9
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions rinja_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,31 @@ impl Config {
template_whitespace: Option<&str>,
config_span: Option<Span>,
) -> Result<&'static Config, CompileError> {
static CACHE: ManuallyDrop<OnceLock<OnceMap<OwnedConfigKey, Arc<Config>>>> =
static CACHE: ManuallyDrop<OnceLock<OnceMap<OwnedConfigKey, &'static Config>>> =
ManuallyDrop::new(OnceLock::new());

let config = CACHE.get_or_init(OnceMap::new).get_or_try_insert_ref(
CACHE.get_or_init(OnceMap::new).get_or_try_insert_ref(
&ConfigKey {
source: source.into(),
config_path: config_path.map(Cow::Borrowed),
template_whitespace: template_whitespace.map(Cow::Borrowed),
},
config_span,
ConfigKey::to_owned,
|config_span, key| match Config::new_uncached(key.clone(), config_span) {
Ok(config) => Ok((Arc::clone(&config), config)),
Err(err) => Err(err),
|config_span, key| {
let config = Config::new_uncached(key.clone(), config_span)?;
let config = &*Box::leak(Box::new(config));
Ok((config, config))
},
|_, _, value| Arc::clone(value),
)?;
// SAFETY: an inserted `Config` will never be evicted
Ok(unsafe { transmute::<&Config, &'static Config>(config.as_ref()) })
|_, _, config| config,
)
}
}

impl Config {
fn new_uncached(
key: OwnedConfigKey,
config_span: Option<Span>,
) -> Result<Arc<Config>, CompileError> {
) -> Result<Config, CompileError> {
// SAFETY: the resulting `Config` will keep a reference to the `key`
let eternal_key =
unsafe { transmute::<&ConfigKey<'_>, &'static ConfigKey<'static>>(key.borrow()) };
Expand Down Expand Up @@ -192,14 +190,14 @@ impl Config {
));
}

Ok(Arc::new(Config {
Ok(Config {
dirs,
syntaxes,
default_syntax,
escapers,
whitespace,
_key: key,
}))
})
}

pub(crate) fn find_template(
Expand Down

0 comments on commit 538c0e9

Please sign in to comment.