add some cstring constructors and give cstring data to Component trait

This commit is contained in:
bgkillas 2025-08-17 11:57:51 -04:00
parent 448ddcaeab
commit 4b0f5d15cc
3 changed files with 194 additions and 6 deletions

View file

@ -154,6 +154,33 @@ impl Ord for StdString {
}
#[repr(transparent)]
pub struct CString(pub *const u8);
impl From<&str> for CString {
fn from(value: &str) -> Self {
let value = value.to_owned() + "\0";
let str = Box::leak(Box::new(value));
CString(str.as_ptr())
}
}
impl CString {
pub const fn from_str(value: &'static str) -> Self {
CString(value.as_ptr())
}
}
#[test]
fn test_str() {
let str;
let str2;
{
str = CString::from("test");
str2 = const { CString::from_str("test\0") };
assert_eq!(str.to_string(), "test");
assert_eq!(str2.to_string(), "test")
}
assert_eq!(str.to_string(), "test");
assert_eq!(str2.to_string(), "test");
assert_eq!(WalletComponent::NAME, WalletComponent::C_NAME.to_string());
assert_eq!(WalletComponent::NAME, WalletComponent::STD_NAME.to_string());
}
impl Display for CString {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.0.is_null() {

View file

@ -168,12 +168,7 @@ impl ComponentBuffer {
.find_map(|a| unsafe { a.as_ref().map(|a| a.vtable) })
.unwrap_or(&ComponentVTable {}),
local_id: self.component_list.len(),
type_name: self
.component_list
.as_ref()
.iter()
.find_map(|a| unsafe { a.as_ref().map(|a| CString(a.type_name.0)) })
.unwrap_or(CString(ptr::null_mut())),
type_name: C::C_NAME,
type_id,
id,
enabled: false,

File diff suppressed because it is too large Load diff