Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions flecs_ecs/src/core/components/cached_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,33 @@ impl<'a, T: ComponentId + DataComponent> CachedRef<'a, T> {
}
.is_null()
}

/// Try to mutably borrow component from ref.
pub fn try_borrow_mut(&mut self) -> Option<&mut T> {
NonNull::new(unsafe {
sys::ecs_ref_get_id(
self.world.world_ptr_mut(),
&mut self.component_ref,
self.component_ref.id,
) as *mut T
})
.map(|mut t| unsafe { t.as_mut() })
}

/// Mutably borrow component from ref.
///
/// # Panics
///
/// Panics if the the ref does not refer to a component.
pub fn borrow_mut(&mut self) -> &mut T {
NonNull::new(unsafe {
sys::ecs_ref_get_id(
self.world.world_ptr_mut(),
&mut self.component_ref,
self.component_ref.id,
) as *mut T
})
.map(|mut t| unsafe { t.as_mut() })
.expect("Component not found, use try_get if you want to handle this case")
}
}