Replace all parse_mount_info usage with Rust

This commit is contained in:
topjohnwu
2024-02-27 03:14:26 -08:00
parent af6965eefa
commit 1a70796339
8 changed files with 278 additions and 61 deletions

View File

@@ -2,9 +2,10 @@ use std::cmp::min;
use std::ffi::{CStr, FromBytesWithNulError, OsStr};
use std::fmt::{Arguments, Debug, Display, Formatter, Write};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str::{Utf8Chunks, Utf8Error};
use std::{fmt, mem, slice, str};
use std::os::unix::ffi::OsStrExt;
use cxx::{type_id, ExternType};
use libc::c_char;
@@ -105,7 +106,7 @@ trait AsUtf8CStr {
// Implementation for Utf8CString
trait StringExt {
pub trait StringExt {
fn nul_terminate(&mut self) -> &mut [u8];
}
@@ -122,6 +123,21 @@ impl StringExt for String {
}
}
impl StringExt for PathBuf {
#[allow(mutable_transmutes)]
fn nul_terminate(&mut self) -> &mut [u8] {
self.reserve(1);
// SAFETY: the PathBuf is reserved to have enough capacity to fit in the null byte
// SAFETY: the null byte is explicitly added outside of the PathBuf's length
unsafe {
let bytes: &mut [u8] = mem::transmute(self.as_mut_os_str().as_bytes());
let buf = slice::from_raw_parts_mut(bytes.as_mut_ptr(), bytes.len() + 1);
*buf.get_unchecked_mut(bytes.len()) = b'\0';
buf
}
}
}
#[derive(Default)]
pub struct Utf8CString(String);