yoke/
utils.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use crate::Yokeable;
6use core::mem;
7
8/// This method casts `yokeable` between `&'a mut Y<'static>` and `&'a mut Y<'a>`,
9/// and passes it to `f`.
10///
11/// See [`Yokeable::transform_mut`] for why this is safe, noting that no `'static` return type
12/// can leak data from the cart or Yokeable.
13#[inline]
14pub(crate) fn transform_mut_yokeable<'a, Y, F, R>(yokeable: &'a mut Y, f: F) -> R
15where
16    Y: Yokeable<'a>,
17    // be VERY CAREFUL changing this signature, it is very nuanced
18    F: 'static + for<'b> FnOnce(&'b mut Y::Output) -> R,
19    R: 'static,
20{
21    // Cast away the lifetime of `Y`
22    // Safety: this is equivalent to f(transmute(yokeable)), and the documentation of
23    // [`Yokeable::transform_mut`] and this function explain why doing so is sound.
24    unsafe { f(mem::transmute::<&'a mut Y, &'a mut Y::Output>(yokeable)) }
25}