Skip to content

[Feature request] Ergonomic borrow structures with static lifetime for baked data providers #6053

@csmulhern

Description

@csmulhern

As far as I can tell, you can only get TypeBorrowed<'static> versions of structs without unsafe when using compile_data.

E.g. when using ComposingNormalizer, all interesting methods are on ComposingNormalizerBorrowed. ComposingNormalizer::new_nfc returns a ComposingNormalizerBorrowed<'static> for use with compile_data.

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 ComposingNormalizer, you're then forced to call ComposingNormalizer ::as_borrowed whenever you need to actually use the normalizer.

Alternatively, you can use unsafe to create a ComposingNormalizerBorrowed<'static>, although it's unclear if this is actually safe or not:

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)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions