pub trait Borrow<'a> {
type View;
fn borrow(
world: &'a World,
last_run: Option<u32>,
current: u32
) -> Result<Self::View, GetStorage>;
}
Expand description
Allows a type to be borrowed by World::borrow
, World::run
and workloads.
Example of manual implementation:
use shipyard::{Borrow, IntoBorrow, View, UniqueView, World};
struct CameraView<'v> {
camera: UniqueView<'v, Camera>,
position: View<'v, Position>,
}
// There shouldn't be any lifetime on this struct.
// If the custom view has generics, PhantomData can be used to make the compiler happy.
struct CameraViewBorrower {}
impl<'v> Borrow<'v> for CameraViewBorrower {
type View = CameraView<'v>;
fn borrow(
world: &'v World,
last_run: Option<u32>,
current: u32,
) -> Result<Self::View, shipyard::error::GetStorage> {
Ok(CameraView {
camera: <UniqueView<'v, Camera> as IntoBorrow>::Borrow::borrow(world, last_run, current)?,
position: <View<'v, Position> as IntoBorrow>::Borrow::borrow(world, last_run, current)?,
})
}
}