diff --git a/src/sdk/output/rust.rs b/src/sdk/output/rust.rs index f5d699d..29b8b93 100644 --- a/src/sdk/output/rust.rs +++ b/src/sdk/output/rust.rs @@ -156,22 +156,41 @@ pub fn generate_class(class: &Class, _sdk: &Sdk, w: &mut W) -> anyhow: let name = class.rust_name(); writeln!(w, "#[repr(transparent)]")?; - writeln!(w, "#[derive(Debug, Eq, PartialEq, Clone)]")?; if !is_class { - writeln!(w, "pub struct {name}(UnsafeCell<[u8; {size}]>);")?; + writeln!(w, "#[derive(Debug)]")?; + writeln!(w, "pub struct {name}(pub UnsafeCell<[u8; {size}]>);")?; writeln!(w, "unsafe impl Send for {name} {{}}")?; writeln!(w, "unsafe impl Sync for {name} {{}}")?; + write!( + w, + r#" +impl Eq for {name} {{}} + +impl PartialEq for {name} {{ + fn eq(&self, other: &Self) -> bool {{ + unsafe {{ &*self.0.get() }}.eq(unsafe {{ &*other.0.get() }}) + }} +}} + +impl Clone for {name} {{ + fn clone(&self) -> Self {{ + Self(UnsafeCell::new(unsafe {{ &*self.0.get() }}.clone())) + }} +}} +"# + )?; + write!( w, r#" impl AsPtr for {name} {{ fn as_ptr(&self) -> *const u8 {{ - self.0.get() as _ + self.0.get().cast() }} fn as_mut_ptr(&self) -> *mut u8 {{ - self.0.get() + self.0.get().cast() }} }} "# @@ -188,7 +207,8 @@ impl {name} {{ "# )?; } else { - writeln!(w, "pub struct {name}(NonNull>);")?; + writeln!(w, "#[derive(Debug, Eq, PartialEq, Copy, Clone)]")?; + writeln!(w, "pub struct {name}(pub NonNull>);")?; writeln!(w, "unsafe impl Send for {name} {{}}")?; writeln!(w, "unsafe impl Sync for {name} {{}}")?; @@ -201,7 +221,7 @@ impl AsUObject for {name} {{ UObject(self.0) }} - fn from_uobject(obj: UObject) -> Self {{ + fn from_uobject(obj: &UObject) -> Self {{ Self(obj.0) }} }} @@ -313,13 +333,8 @@ fn generate_find_object(name: &str, w: &mut W) -> anyhow::Result<()> { r#" {{ static OBJECT: OnceCell> = OnceCell::new(); - *OBJECT.get_or_init(|| {{ - match GOBJECTS - .read() - .unwrap() - .as_objects() - .unwrap() - .find_class(&"{name}") {{ + OBJECT.get_or_init(|| {{ + match find_object("{name}") {{ Some(class) => {{Some(class)}}, None => {{ log::error!("static object {name} not found!"); @@ -366,7 +381,7 @@ pub fn generate_method( writeln!(w, " {{")?; - write!(w, "let func: UFunction = ")?; + write!(w, "let func: UFunction =")?; generate_find_object(&method.full_name, w)?; writeln!(w, ".expect(\"function '{}' not found\");", method.full_name)?;