Remove Utf8CStrBuffer

This commit is contained in:
topjohnwu 2025-04-23 18:07:40 -07:00 committed by John Wu
parent 5c1cb13472
commit f44d044095

View File

@ -4,7 +4,7 @@ use std::borrow::Borrow;
use std::cmp::min; use std::cmp::min;
use std::ffi::{CStr, FromBytesWithNulError, OsStr}; use std::ffi::{CStr, FromBytesWithNulError, OsStr};
use std::fmt::{Debug, Display, Formatter, Write}; use std::fmt::{Debug, Display, Formatter, Write};
use std::ops::{Deref, DerefMut}; use std::ops::Deref;
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::Utf8Error; use std::str::Utf8Error;
@ -66,63 +66,6 @@ pub mod buf {
} }
} }
// Union type for storing a Utf8CStrBuf value
pub enum Utf8CStrBuffer<'a, const N: usize> {
Array(Utf8CStrBufArr<N>),
Reference(Utf8CStrBufRef<'a>),
Dynamic(Utf8CString),
Wrap(&'a mut dyn Utf8CStrBuf),
}
impl<'a, const N: usize> Deref for Utf8CStrBuffer<'a, N> {
type Target = dyn Utf8CStrBuf + 'a;
fn deref(&self) -> &Self::Target {
match self {
Utf8CStrBuffer::Array(s) => s,
Utf8CStrBuffer::Reference(s) => s,
Utf8CStrBuffer::Dynamic(s) => s,
Utf8CStrBuffer::Wrap(s) => *s,
}
}
}
impl<const N: usize> DerefMut for Utf8CStrBuffer<'_, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Utf8CStrBuffer::Array(s) => s,
Utf8CStrBuffer::Reference(s) => s,
Utf8CStrBuffer::Dynamic(s) => s,
Utf8CStrBuffer::Wrap(s) => *s,
}
}
}
impl From<Utf8CString> for Utf8CStrBuffer<'static, 0> {
fn from(value: Utf8CString) -> Self {
Utf8CStrBuffer::Dynamic(value)
}
}
impl<const N: usize> From<Utf8CStrBufArr<N>> for Utf8CStrBuffer<'static, N> {
fn from(value: Utf8CStrBufArr<N>) -> Self {
Utf8CStrBuffer::Array(value)
}
}
impl<'a> From<&'a mut dyn Utf8CStrBuf> for Utf8CStrBuffer<'a, 0> {
fn from(value: &'a mut dyn Utf8CStrBuf) -> Self {
Utf8CStrBuffer::Wrap(value)
}
}
impl Default for Utf8CStrBuffer<'static, 4096> {
fn default() -> Self {
Utf8CStrBuffer::Array(buf::default())
}
}
// Trait definitions // Trait definitions
pub trait Utf8CStrBuf: Write + AsRef<Utf8CStr> + Deref<Target = Utf8CStr> { pub trait Utf8CStrBuf: Write + AsRef<Utf8CStr> + Deref<Target = Utf8CStr> {