stable_osstring_encoding/traits.rs
1use crate::{EncodingWidth, StableOsString};
2use std::borrow::Cow;
3
4
5
6/// Converts a type to an encoding that is stable across rust compiler versions
7pub trait ToRawStableEncoding {
8 /// Converts a type to an encoding that is stable across rust compiler versions
9 fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth>;
10}
11
12/// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
13pub trait IntoRawStableEncoding {
14 /// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
15 fn into_raw_stable_encoding(self) -> Vec<EncodingWidth>;
16}
17
18/// Crates an `OsString` from an encoding that is stable across rust compiler versions, bypassing data copies if possible
19pub trait FromRawStableEncoding {
20 /// Converts an `OsString` from an encoding that is stable across rust compiler versions, bypassing data copies if possible
21 ///
22 /// This takes `Into<Cow<[EncodingWidth]>>` so that either a slice can be passed (which always allocates and copies data) or a vec can be passed (which might be able to skip allocating and copying data)
23 ///
24 /// # Safety
25 ///
26 /// The given bytes must be compatible with the underlying of the platform's `OsStr` encoding (reminder: this crate only make it safe to pass data between different rust versions)
27 unsafe fn from_raw_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self;
28}
29
30
31
32/// Converts a type to an encoding that is stable across rust compiler versions
33pub trait ToStableEncoding {
34 /// Converts a type to an encoding that is stable across rust compiler versions
35 fn to_stable_encoding(&self) -> StableOsString;
36}
37
38/// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
39pub trait IntoStableEncoding {
40 /// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
41 fn into_stable_encoding(self) -> StableOsString;
42}