Skip to main content

PmpxStr

Struct PmpxStr 

Source
#[repr(C)]
pub struct PmpxStr { pub ptr: *const u8, pub len: usize, }
Expand description

跨边界字符串:指针 + 长度,不要求 NUL 结尾。

§这是字节,不是 String

ptr/len 描述的是一段裸字节。它可能是路径、可能是命令行参数, 所以在 Unix 上完全可以不是合法 UTF-8 —— 转换走 std::os::unix::ffi::OsStrExt, 无损。

只有 PmpxStr 明确要求是文本时(例如 matched 里的文件名)才会做 UTF-8 校验, 校验失败返回 PMPX_ERR_INVALID_ARGS 而不是 UB。

§为什么带长度而不是靠 NUL

str::as_ptr() 得到的指针不保证后面跟着 NUL。指望它等于把 UB 写进设计。 带长度是唯一诚实的做法,顺带还省掉一次 strlen。

Fields§

§ptr: *const u8

起始地址。len == 0 时可以是空指针。

§len: usize

字节长度。

Implementations§

Source§

impl PmpxStr

Source

pub const EMPTY: PmpxStr

空。len == 0 且指针为空 —— 在 cwd 里表示“没有覆盖“。

Source

pub const fn from_static(s: &'static str) -> Self

从一段 'static 文本构造。

const 是关键:export! 生成的 static vtable 要在编译期填好这两个字段。

Source

pub const fn is_empty(&self) -> bool

是不是空的。

Trait Implementations§

Source§

impl Clone for PmpxStr

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for PmpxStr

Source§

impl Debug for PmpxStr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Sync for PmpxStr

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.