-
As far as I can tell, you can only get E.g. when using When using a baked data provider, the only alternative I see is to do e.g. let normalizer = icu::normalizer::ComposingNormalizer::try_new_nfc_unstable(&BakedDataProvider).unwrap(); If implementing a struct that owns a Alternatively, you can use unsafe to create a let normalizer = icu::normalizer::ComposingNormalizer::try_new_nfc_unstable(&BakedDataProvider).unwrap();
let normalizer: icu::normalizer::ComposingNormalizerBorrowed<'static> =
unsafe { std::mem::transmute(normalizer.as_borrowed()) }; Am I doing something wrong? Is there a more ergonomic API that could be added here (or clarification added in the docs whether extending the lifetime here is safe or not)? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You are correct that the only way to create a In general, casting the return value of |
Beta Was this translation helpful? Give feedback.
-
This is intentional: it's a very cheap API that often optimizes out, but it's there to let people consolidate where possible. Don't worry too much about the cost, it's extremely minor where it shows up at all. Prior API design involved
It is not. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the clarification! I'm not overly worried about the cost, but I wasn't sure how to implement a wrapper around e.g. ComposingNormalizer due to lifetime issues. I.e. calling pub struct ComposingNormalizer {
normalizer: icu::normalizer::ComposingNormalizer
}
impl ComposingNormalizer {
#[inline]
pub fn normalize_iter<'a, T>(&'a self, iterator: T) -> impl Iterator<Item = char> + 'a
where
T: Iterator<Item = char> + 'a
{
let borrowed_normalizer = self.normalizer.as_borrowed();
return borrowed_normalizer.normalize_iter(iterator);
}
}
Ah, I see. Interesting. I would have thought using a baked data provider would leverage all static data too, but I guess because it still goes through the DataProvider interface, it's limited by that API? Are there any plans to provide similar ergonomics for baked data providers? |
Beta Was this translation helpful? Give feedback.
You are correct that the only way to create a
ComposingNormalizerBorrowed<'static>
is to use compiled data; the reason is that compiled data exports singleton statics that bypass the DataProvider trait.In general, casting the return value of
as_borrowed
to'static
is unsound (unless perhaps you have it saved in a static OnceLock or something). However, callingas_borrowed
is very cheap (though not 100% free: #5187). The idea is that if you need to call one of the terminal functions such asnormalize
multiple times, you can callas_borrowed
just once.