#[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§
Trait Implementations§
impl Copy for PmpxStr
impl Sync for PmpxStr
Auto Trait Implementations§
impl !Send for PmpxStr
impl Freeze for PmpxStr
impl RefUnwindSafe for PmpxStr
impl Unpin for PmpxStr
impl UnsafeUnpin for PmpxStr
impl UnwindSafe for PmpxStr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more