mirror of
https://github.com/IntQuant/noita_entangled_worlds.git
synced 2025-10-19 07:03:16 +00:00
add some cstring constructors and give cstring data to Component trait
This commit is contained in:
parent
448ddcaeab
commit
4b0f5d15cc
3 changed files with 194 additions and 6 deletions
|
@ -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() {
|
||||
|
|
|
@ -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
Loading…
Add table
Add a link
Reference in a new issue