use core::{
fmt,
marker::PhantomData,
ops::{Deref, DerefMut},
};
use crate::ParamError;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct DefaultResourceTag;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ResourceId(usize);
impl ResourceId {
pub const fn new(value: usize) -> Self {
Self(value)
}
pub const fn get(self) -> usize {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessKind {
None,
Shared,
Exclusive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResourceAccess {
resource: ResourceId,
kind: AccessKind,
}
impl ResourceAccess {
pub const fn none() -> Self {
Self {
resource: ResourceId::new(0),
kind: AccessKind::None,
}
}
pub const fn shared(resource: ResourceId) -> Self {
Self {
resource,
kind: AccessKind::Shared,
}
}
pub const fn exclusive(resource: ResourceId) -> Self {
Self {
resource,
kind: AccessKind::Exclusive,
}
}
pub const fn resource(self) -> ResourceId {
self.resource
}
pub const fn kind(self) -> AccessKind {
self.kind
}
const fn conflicts(self, other: Self) -> bool {
if matches!(self.kind, AccessKind::None) || matches!(other.kind, AccessKind::None) {
return false;
}
self.resource.0 == other.resource.0
&& (matches!(self.kind, AccessKind::Exclusive)
|| matches!(other.kind, AccessKind::Exclusive))
}
}
pub unsafe trait ResourceProvider<T: ?Sized + 'static, Tag: 'static = DefaultResourceTag> {
const ID: ResourceId;
unsafe fn get(resources: *mut Self) -> *mut T;
}
#[repr(transparent)]
pub struct Res<'resources, T: ?Sized + 'static, Tag: 'static = DefaultResourceTag> {
value: &'resources T,
marker: PhantomData<fn() -> Tag>,
}
impl<T, Tag> Clone for Res<'_, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
fn clone(&self) -> Self {
*self
}
}
impl<T, Tag> Copy for Res<'_, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
}
impl<'resources, T, Tag> Res<'resources, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
pub const fn into_inner(self) -> &'resources T {
self.value
}
}
impl<T, Tag> Deref for Res<'_, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<T, Tag> fmt::Debug for Res<'_, T, Tag>
where
T: fmt::Debug + ?Sized + 'static,
Tag: 'static,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(formatter)
}
}
#[repr(transparent)]
pub struct ResMut<'resources, T: ?Sized + 'static, Tag: 'static = DefaultResourceTag> {
value: &'resources mut T,
marker: PhantomData<fn() -> Tag>,
}
impl<'resources, T, Tag> ResMut<'resources, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
pub fn into_inner(self) -> &'resources mut T {
self.value
}
}
impl<T, Tag> Deref for ResMut<'_, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<T, Tag> DerefMut for ResMut<'_, T, Tag>
where
T: ?Sized + 'static,
Tag: 'static,
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.value
}
}
impl<T, Tag> fmt::Debug for ResMut<'_, T, Tag>
where
T: fmt::Debug + ?Sized + 'static,
Tag: 'static,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(formatter)
}
}
pub unsafe trait SystemParam<R> {
type Item<'resources>;
const ACCESS: ResourceAccess;
unsafe fn fetch<'resources>(
resources: *mut R,
) -> Result<Self::Item<'resources>, crate::FetchError>;
}
unsafe impl<'param, R, T, Tag> SystemParam<R> for Res<'param, T, Tag>
where
R: ResourceProvider<T, Tag>,
T: 'static,
Tag: 'static,
{
type Item<'resources> = Res<'resources, T, Tag>;
const ACCESS: ResourceAccess = ResourceAccess::shared(R::ID);
unsafe fn fetch<'resources>(
resources: *mut R,
) -> Result<Self::Item<'resources>, crate::FetchError> {
let value = unsafe { R::get(resources) };
if value.is_null() || !value.is_aligned() {
return Err(crate::FetchError::InvalidPointer);
}
Ok(Res {
value: unsafe { &*value },
marker: PhantomData,
})
}
}
unsafe impl<'param, R, T, Tag> SystemParam<R> for ResMut<'param, T, Tag>
where
R: ResourceProvider<T, Tag>,
T: 'static,
Tag: 'static,
{
type Item<'resources> = ResMut<'resources, T, Tag>;
const ACCESS: ResourceAccess = ResourceAccess::exclusive(R::ID);
unsafe fn fetch<'resources>(
resources: *mut R,
) -> Result<Self::Item<'resources>, crate::FetchError> {
let value = unsafe { R::get(resources) };
if value.is_null() || !value.is_aligned() {
return Err(crate::FetchError::InvalidPointer);
}
Ok(ResMut {
value: unsafe { &mut *value },
marker: PhantomData,
})
}
}
pub(crate) trait ParamSet<R> {
type Item<'resources>
where
R: 'resources;
fn fetch<'resources>(
resources: &'resources mut R,
) -> Result<Self::Item<'resources>, ParamError>;
}
macro_rules! impl_param_set {
($($param:ident => $value:ident @ $index:expr),+ $(,)?) => {
impl<R, $($param),+> ParamSet<R> for ($($param,)+)
where
$($param: SystemParam<R>,)+
{
type Item<'resources> = ($($param::Item<'resources>,)+)
where
R: 'resources;
fn fetch<'resources>(
resources: &'resources mut R,
) -> Result<Self::Item<'resources>, ParamError> {
let accesses = [$($param::ACCESS),+];
validate_accesses(&accesses)?;
let resources = resources as *mut R;
$(
let $value = unsafe { $param::fetch::<'resources>(resources) }
.map_err(|error| ParamError::Fetch {
index: $index,
error,
})?;
)+
Ok(($($value,)+))
}
}
};
}
impl_param_set!(P0 => p0 @ 0);
impl_param_set!(P0 => p0 @ 0, P1 => p1 @ 1);
impl_param_set!(P0 => p0 @ 0, P1 => p1 @ 1, P2 => p2 @ 2);
impl_param_set!(P0 => p0 @ 0, P1 => p1 @ 1, P2 => p2 @ 2, P3 => p3 @ 3);
impl_param_set!(
P0 => p0 @ 0,
P1 => p1 @ 1,
P2 => p2 @ 2,
P3 => p3 @ 3,
P4 => p4 @ 4,
);
impl_param_set!(
P0 => p0 @ 0,
P1 => p1 @ 1,
P2 => p2 @ 2,
P3 => p3 @ 3,
P4 => p4 @ 4,
P5 => p5 @ 5,
);
impl_param_set!(
P0 => p0 @ 0,
P1 => p1 @ 1,
P2 => p2 @ 2,
P3 => p3 @ 3,
P4 => p4 @ 4,
P5 => p5 @ 5,
P6 => p6 @ 6,
);
impl_param_set!(
P0 => p0 @ 0,
P1 => p1 @ 1,
P2 => p2 @ 2,
P3 => p3 @ 3,
P4 => p4 @ 4,
P5 => p5 @ 5,
P6 => p6 @ 6,
P7 => p7 @ 7,
);
pub(crate) fn validate_accesses(accesses: &[ResourceAccess]) -> Result<(), ParamError> {
let mut second = 1;
while second < accesses.len() {
let mut first = 0;
while first < second {
if accesses[first].conflicts(accesses[second]) {
return Err(ParamError::AccessConflict {
first: first as u8,
second: second as u8,
resource: accesses[first].resource,
});
}
first += 1;
}
second += 1;
}
Ok(())
}