From 0d315ab0e4b3539d23e3680a53c13d65b961784d Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Thu, 18 Dec 2025 15:53:28 +0100 Subject: [PATCH] Implement Default for CStr and CString and add constructor to CString --- src/raw/cstr.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/raw/cstr.rs b/src/raw/cstr.rs index 8db13fcc..51956941 100644 --- a/src/raw/cstr.rs +++ b/src/raw/cstr.rs @@ -92,6 +92,12 @@ impl CStr { } } +impl Default for &CStr { + fn default() -> Self { + cstr!("") + } +} + impl PartialEq for CStr { fn eq(&self, other: &CStr) -> bool { self.as_str() == other.as_str() @@ -233,7 +239,7 @@ pub use cstr; /// /// Like `CStr`, this differs from [`std::ffi::CString`] in that it is required to be valid UTF-8, /// and does not include the nul terminator in the buffer. -#[derive(Clone, Eq)] +#[derive(Clone, Eq, Default)] #[repr(transparent)] pub struct CString { data: String, @@ -258,6 +264,11 @@ impl TryFrom<&str> for CString { } impl CString { + /// Creates a new empty `CString`. + pub fn new() -> Self { + Self::default() + } + pub(crate) fn from_string_unchecked(data: String) -> Self { Self { data } }