sapi_lite/stt/semantics/
string.rs

1use std::borrow::Cow;
2use std::ffi::{OsStr, OsString};
3
4/// A string type that can be borrowed as an [`OsStr`](std::ffi::OsStr).
5pub trait SemanticString {
6    /// Borrows the string as [`OsStr`](std::ffi::OsStr).
7    fn as_os_str(&self) -> &OsStr;
8}
9
10impl<'s> SemanticString for &'s str {
11    fn as_os_str(&self) -> &OsStr {
12        OsStr::new(self)
13    }
14}
15
16impl SemanticString for String {
17    fn as_os_str(&self) -> &OsStr {
18        OsStr::new(self)
19    }
20}
21
22impl<'s> SemanticString for &'s OsStr {
23    fn as_os_str(&self) -> &OsStr {
24        self
25    }
26}
27
28impl SemanticString for OsString {
29    fn as_os_str(&self) -> &OsStr {
30        self.as_os_str()
31    }
32}
33
34impl<'s> SemanticString for Cow<'s, str> {
35    fn as_os_str(&self) -> &OsStr {
36        match self {
37            Cow::Borrowed(s) => s.as_os_str(),
38            Cow::Owned(s) => s.as_os_str(),
39        }
40    }
41}
42
43impl<'s> SemanticString for Cow<'s, OsStr> {
44    fn as_os_str(&self) -> &OsStr {
45        match self {
46            Cow::Borrowed(s) => *s,
47            Cow::Owned(s) => s.as_os_str(),
48        }
49    }
50}