Skip to main content

Resource

Trait Resource 

pub trait Resource: Component { }
Expand description

A type that can be inserted into a World as a singleton.

You can access resource data in systems using the Res and ResMut system parameters

Only one resource of each type can be stored in a World at any given time.

§Examples

#[derive(Resource)]
struct MyResource { value: u32 }

world.insert_resource(MyResource { value: 42 });

fn read_resource_system(resource: Res<MyResource>) {
    assert_eq!(resource.value, 42);
}

fn write_resource_system(mut resource: ResMut<MyResource>) {
    assert_eq!(resource.value, 42);
    resource.value = 0;
    assert_eq!(resource.value, 0);
}

§!Sync Resources

A !Sync type cannot implement Resource. However, it is possible to wrap a Send but not Sync type in SyncCell or the currently unstable Exclusive to make it Sync. This forces only having mutable access (&mut T only, never &T), but makes it safe to reference across multiple threads.

This will fail to compile since RefCell is !Sync.


#[derive(Resource)]
struct NotSync {
   counter: RefCell<usize>,
}

This will compile since the RefCell is wrapped with SyncCell.

use bevy_platform::cell::SyncCell;

#[derive(Resource)]
struct ActuallySync {
   counter: SyncCell<RefCell<usize>>,
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Resource for EventLoopWaker
where Self: Send + Sync + 'static,

Source§

impl Resource for CommandQueue
where Self: Send + Sync + 'static,

Source§

impl Resource for CommandReceiver
where Self: Send + Sync + 'static,

Source§

impl Resource for CommandRegistry
where Self: Send + Sync + 'static,

Source§

impl Resource for A11yAnnouncementQueue
where Self: Send + Sync + 'static,

Source§

impl Resource for A11yContextMenuRequests
where Self: Send + Sync + 'static,

Source§

impl Resource for A11yRootLabel
where Self: Send + Sync + 'static,

Source§

impl Resource for A11yScrollIntoViewRequests
where Self: Send + Sync + 'static,

Source§

impl Resource for DefaultLayoutDirection
where Self: Send + Sync + 'static,

Source§

impl Resource for MemoryBudget
where Self: Send + Sync + 'static,

Source§

impl Resource for PendingA11yUpdate
where Self: Send + Sync + 'static,

Source§

impl Resource for RootWindowEntity
where Self: Send + Sync + 'static,

Source§

impl Resource for StyleManager
where Self: Send + Sync + 'static,

Source§

impl Resource for WindowDragRequest
where Self: Send + Sync + 'static,

Source§

impl Resource for CursorRequest
where Self: Send + Sync + 'static,

Source§

impl Resource for EscapePressCancel
where Self: Send + Sync + 'static,

Source§

impl Resource for FocusTracker
where Self: Send + Sync + 'static,

Source§

impl Resource for ImeRequest
where Self: Send + Sync + 'static,

Source§

impl Resource for ModifiersState
where Self: Send + Sync + 'static,

Source§

impl Resource for PendingFileDrops
where Self: Send + Sync + 'static,

Source§

impl Resource for PointerState
where Self: Send + Sync + 'static,

Source§

impl Resource for ScrollbarInteraction
where Self: Send + Sync + 'static,

Source§

impl Resource for NodeHandles
where Self: Send + Sync + 'static,

Source§

impl Resource for PreviousScene
where Self: Send + Sync + 'static,

Source§

impl Resource for RetainedScene
where Self: Send + Sync + 'static,

Source§

impl Resource for Palette
where Self: Send + Sync + 'static,

Source§

impl Resource for PropertyStore
where Self: Send + Sync + 'static,

Source§

impl Resource for AnimationsActive
where Self: Send + Sync + 'static,

Source§

impl Resource for ExtractContextCache
where Self: Send + Sync + 'static,

Source§

impl Resource for FrameDamage
where Self: Send + Sync + 'static,

Source§

impl Resource for FrameDirty
where Self: Send + Sync + 'static,

Source§

impl Resource for HiddenExtracts
where Self: Send + Sync + 'static,

Source§

impl Resource for OverlayOpenOrder
where Self: Send + Sync + 'static,

Source§

impl Resource for RenderEntityMap
where Self: Send + Sync + 'static,

Source§

impl Resource for SurfaceCapture
where Self: Send + Sync + 'static,

Source§

impl Resource for Viewport
where Self: Send + Sync + 'static,

Source§

impl Resource for ArraySignals
where Self: Send + Sync + 'static,

Source§

impl Resource for Signals
where Self: Send + Sync + 'static,

Source§

impl Resource for Tick
where Self: Send + Sync + 'static,

§

impl Resource for Schedules
where Schedules: Send + Sync + 'static,

§

impl Resource for DefaultQueryFilters
where DefaultQueryFilters: Send + Sync + 'static,

§

impl Resource for FallbackErrorHandler
where FallbackErrorHandler: Send + Sync + 'static,

§

impl Resource for MessageRegistry
where MessageRegistry: Send + Sync + 'static,

§

impl Resource for RegisteredSystemDespawner
where RegisteredSystemDespawner: Send + Sync + 'static,

§

impl Resource for Stepping
where Stepping: Send + Sync + 'static,

§

impl<M> Resource for Messages<M>
where M: Message, Messages<M>: Send + Sync + 'static,

§

impl<S> Resource for CachedSystemId<S>
where CachedSystemId<S>: Send + Sync + 'static,