Skip to content
This repository was archived by the owner on Feb 19, 2026. It is now read-only.
Merged
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
39 changes: 27 additions & 12 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,22 @@ where

self.refs.clear();

let types: &'w [(u16, <S::Fetch as Fetch<'w>>::Ty)] = unsafe { transmute(&*self.types) };
let refs: &'w mut Vec<<S::Fetch as Fetch<'w>>::Ref> = unsafe { transmute(&mut self.refs) };
let ptrs: &'w mut [Option<<S::Fetch as Fetch<'w>>::Ptr>] =
unsafe { transmute(&mut *self.ptrs) };
let ref_ = QueryRef {
world,
types: unsafe { transmute(&*self.types) },
refs: unsafe { transmute(&mut self.refs) },
ptrs: unsafe { transmute(&mut *self.ptrs) },
};

for (idx, ty) in types {
for (idx, ty) in ref_.types {
let archetype = &world.archetypes[*idx as usize];

if archetype.len() != 0 {
refs.push(unsafe { S::Fetch::borrow(archetype, *ty) });
ref_.refs.push(unsafe { S::Fetch::borrow(archetype, *ty) });
}
}

QueryRef {
world,
types,
refs,
ptrs,
}
ref_
}

#[cold]
Expand Down Expand Up @@ -820,6 +817,7 @@ mod tests {
use super::*;

use std::mem::{forget, size_of};
use std::panic::{catch_unwind, AssertUnwindSafe};

fn spawn_three(world: &mut World) {
let ent = world.alloc();
Expand Down Expand Up @@ -923,6 +921,23 @@ mod tests {
let _ = query.borrow(&world);
}

#[test]
fn conflicting_borrow_leaves_world_in_consistent_state() {
let mut world = World::new();

spawn_three(&mut world);

let res = catch_unwind(AssertUnwindSafe(|| {
let mut query = Query::<(&i32, Option<(&mut i32, &bool)>)>::new();
let _ = query.borrow(&world);
}));

assert!(res.is_err());

let mut query = Query::<&mut i32>::new();
let _ = query.borrow(&world);
}

#[test]
fn borrows_can_be_reused() {
let mut world = World::new();
Expand Down