stable_osstring_encoding/traits.rs
1use crate::{EncodingWidth, StableOsString};
2use std::borrow::Cow;
3#[cfg(doc)]
4use std::ffi::{OsStr, OsString};
5
6
7
8/// Converts a type to an encoding that is stable across rust compiler versions
9pub trait ToRawStableEncoding {
10 /// Converts a type to an encoding that is stable across rust compiler versions
11 #[must_use]
12 fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth>;
13}
14
15/// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
16pub trait IntoRawStableEncoding {
17 /// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
18 #[must_use]
19 fn into_raw_stable_encoding(self) -> Vec<EncodingWidth>;
20}
21
22/// Crates an [`OsString`] from an encoding that is stable across rust compiler versions, bypassing data copies if possible
23pub trait FromRawStableEncoding {
24 /// Converts an [`OsString`] from an encoding that is stable across rust compiler versions, bypassing data copies if possible
25 ///
26 /// 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)
27 ///
28 /// # Safety
29 ///
30 /// 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)
31 #[must_use]
32 unsafe fn from_raw_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self;
33}
34
35
36
37/// Converts a type to an encoding that is stable across rust compiler versions
38pub trait ToStableEncoding {
39 /// Converts a type to an encoding that is stable across rust compiler versions
40 #[must_use]
41 fn to_stable_encoding(&self) -> StableOsString;
42}
43
44/// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
45pub trait IntoStableEncoding {
46 /// Converts an owned type to an encoding that is stable across rust compiler versions, bypassing data copies if possible
47 #[must_use]
48 fn into_stable_encoding(self) -> StableOsString;
49}