From 504b84bb06d281f2b8752d124f3015ff91f31983 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 24 Feb 2021 15:18:33 +0100 Subject: Add JointSet::get_mut and get_unknown_gen_mut Fix https://github.com/dimforge/rapier/issues/121 --- src/dynamics/joint/joint_set.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src') diff --git a/src/dynamics/joint/joint_set.rs b/src/dynamics/joint/joint_set.rs index ed3e69a..513aa4b 100644 --- a/src/dynamics/joint/joint_set.rs +++ b/src/dynamics/joint/joint_set.rs @@ -76,6 +76,12 @@ impl JointSet { self.joint_graph.graph.edge_weight(*id) } + /// Gets a mutable reference to the joint with the given handle. + pub fn get_mut(&mut self, handle: JointHandle) -> Option<&mut Joint> { + let id = self.joint_ids.get_mut(handle.0)?; + self.joint_graph.graph.edge_weight_mut(*id) + } + /// Gets the joint with the given handle without a known generation. /// /// This is useful when you know you want the joint at position `i` but @@ -93,6 +99,23 @@ impl JointSet { )) } + /// Gets a mutable reference to the joint with the given handle without a known generation. + /// + /// This is useful when you know you want the joint at position `i` but + /// don't know what is its current generation number. Generation numbers are + /// used to protect from the ABA problem because the joint position `i` + /// are recycled between two insertion and a removal. + /// + /// Using this is discouraged in favor of `self.get_mut(handle)` which does not + /// suffer form the ABA problem. + pub fn get_unknown_gen_mut(&mut self, i: usize) -> Option<(&mut Joint, JointHandle)> { + let (id, handle) = self.joint_ids.get_unknown_gen_mut(i)?; + Some(( + self.joint_graph.graph.edge_weight_mut(*id)?, + JointHandle(handle), + )) + } + /// Iterates through all the joint on this set. pub fn iter(&self) -> impl Iterator { self.joint_graph -- cgit From 69afb2914ac0dbbec6901e59b263115a52a024cf Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 24 Feb 2021 15:59:06 +0100 Subject: less mut MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Crozet --- src/dynamics/joint/joint_set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/dynamics/joint/joint_set.rs b/src/dynamics/joint/joint_set.rs index 513aa4b..1f25cff 100644 --- a/src/dynamics/joint/joint_set.rs +++ b/src/dynamics/joint/joint_set.rs @@ -78,7 +78,7 @@ impl JointSet { /// Gets a mutable reference to the joint with the given handle. pub fn get_mut(&mut self, handle: JointHandle) -> Option<&mut Joint> { - let id = self.joint_ids.get_mut(handle.0)?; + let id = self.joint_ids.get(handle.0)?; self.joint_graph.graph.edge_weight_mut(*id) } -- cgit From 277d74fb1891950a7a80c2052a2b044521d91e2f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 24 Feb 2021 15:59:11 +0100 Subject: less mut MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Crozet --- src/dynamics/joint/joint_set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/dynamics/joint/joint_set.rs b/src/dynamics/joint/joint_set.rs index 1f25cff..b3a0fd9 100644 --- a/src/dynamics/joint/joint_set.rs +++ b/src/dynamics/joint/joint_set.rs @@ -109,7 +109,7 @@ impl JointSet { /// Using this is discouraged in favor of `self.get_mut(handle)` which does not /// suffer form the ABA problem. pub fn get_unknown_gen_mut(&mut self, i: usize) -> Option<(&mut Joint, JointHandle)> { - let (id, handle) = self.joint_ids.get_unknown_gen_mut(i)?; + let (id, handle) = self.joint_ids.get_unknown_gen(i)?; Some(( self.joint_graph.graph.edge_weight_mut(*id)?, JointHandle(handle), -- cgit