yazi/yazi-shared/src/string.rs
2025-10-13 19:50:46 +08:00

37 lines
895 B
Rust

use std::{borrow::Cow, ffi::{OsStr, OsString}};
use crate::url::{UrlBuf, UrlLike};
pub trait IntoStringLossy {
fn into_string_lossy(self) -> String;
}
impl IntoStringLossy for String {
fn into_string_lossy(self) -> String { self }
}
impl IntoStringLossy for &OsStr {
fn into_string_lossy(self) -> String { self.to_string_lossy().into_owned() }
}
impl IntoStringLossy for OsString {
fn into_string_lossy(self) -> String {
match self.to_string_lossy() {
Cow::Owned(s) => s,
Cow::Borrowed(_) => unsafe { String::from_utf8_unchecked(self.into_encoded_bytes()) },
}
}
}
impl IntoStringLossy for Cow<'_, OsStr> {
fn into_string_lossy(self) -> String {
match self {
Cow::Owned(s) => s.into_string_lossy(),
Cow::Borrowed(s) => s.into_string_lossy(),
}
}
}
impl IntoStringLossy for &UrlBuf {
fn into_string_lossy(self) -> String { self.os_str().into_string_lossy() }
}