From b63d0168181f0a8beb1b30c0f361f0dce4e8a714 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 22 Mar 2023 10:21:42 +0100 Subject: [PATCH] Merge parent setter methods --- packages/lib-roblox/src/instance/mod.rs | 35 +++++++------------------ 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/packages/lib-roblox/src/instance/mod.rs b/packages/lib-roblox/src/instance/mod.rs index 1a52222..7665185 100644 --- a/packages/lib-roblox/src/instance/mod.rs +++ b/packages/lib-roblox/src/instance/mod.rs @@ -135,7 +135,7 @@ impl Instance { let new_ref = Self::clone_inner(self.dom_ref, parent_ref); let new_inst = Self::new(new_ref); - new_inst.set_parent_to_nil(); + new_inst.set_parent(None); new_inst } @@ -340,24 +340,7 @@ impl Instance { /** Sets the parent of the instance, if it exists. - ### See Also - * [`Parent`](https://create.roblox.com/docs/reference/engine/classes/Instance#Parent) - on the Roblox Developer Hub - */ - pub fn set_parent(&self, parent: Instance) { - if self.is_root { - panic!("Root instance can not be reparented") - } - - let mut dom = INTERNAL_DOM - .try_write() - .expect("Failed to get write access to target document"); - - dom.transfer_within(self.dom_ref, parent.dom_ref); - } - - /** - Sets the parent of the instance, if it exists, to nil, making it orphaned. + If the provided parent is [`None`] the instance will become orphaned. An orphaned instance is an instance at the root of a weak dom. @@ -365,7 +348,7 @@ impl Instance { * [`Parent`](https://create.roblox.com/docs/reference/engine/classes/Instance#Parent) on the Roblox Developer Hub */ - pub fn set_parent_to_nil(&self) { + pub fn set_parent(&self, parent: Option) { if self.is_root { panic!("Root instance can not be reparented") } @@ -374,9 +357,11 @@ impl Instance { .try_write() .expect("Failed to get write access to target document"); - let nil_parent_ref = dom.root_ref(); + let parent_ref = parent + .map(|parent| parent.dom_ref) + .unwrap_or_else(|| dom.root_ref()); - dom.transfer_within(self.dom_ref, nil_parent_ref); + dom.transfer_within(self.dom_ref, parent_ref); } /** @@ -739,10 +724,8 @@ impl LuaUserData for Instance { ))); } type Parent = Option; - match Parent::from_lua(prop_value, lua)? { - Some(parent) => this.set_parent(parent), - None => this.set_parent_to_nil(), - } + let parent = Parent::from_lua(prop_value, lua)?; + this.set_parent(parent); return Ok(()); } _ => {}