deriving eq and partial eq, and clone for structs

- public access to tuple field
- casting in AsPtr fix
- generate_find_object relies on external implementation
This commit is contained in:
Janis 2023-04-21 23:01:48 +02:00
parent 06c1a519d3
commit 3f16ac51c6

View file

@ -156,22 +156,41 @@ pub fn generate_class<W: Write>(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<UnsafeCell<u8>>);")?;
writeln!(w, "#[derive(Debug, Eq, PartialEq, Copy, Clone)]")?;
writeln!(w, "pub struct {name}(pub NonNull<UnsafeCell<u8>>);")?;
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<W: Write>(name: &str, w: &mut W) -> anyhow::Result<()> {
r#"
{{
static OBJECT: OnceCell<Option<UObject>> = 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<W: Write>(
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)?;