use std::{any::type_name, marker::PhantomData, num::NonZeroUsize, fmt::Debug};
#[derive(PartialEq)]
pub struct CondAddr<T>{
taskid: TaskId,
section: Section,
argidx: ArgIdx<T>
}
impl<T> CondAddr<T> {
pub const NONE: Self = Self {
taskid: TaskId::NONE,
section: Section::Input,
argidx: ArgIdx::AINONE,
};
pub(crate) const fn new<const I:u8>()->Self {
Self {
taskid: TaskId::NONE,
section: Section::Input,
argidx: ArgIdx::const_new::<I>(),
}
}
}
impl<T> CondAddr<T> {
#[inline]
pub const fn taskid(&self)->TaskId {
self.taskid
}
#[inline]
pub const fn section(&self)->&Section {
&self.section
}
#[inline]
pub const fn argidx(&self)->&ArgIdx<T> {
&self.argidx
}
#[inline]
pub fn set(&mut self, id:TaskId, section: Section, i:ArgIdx<T>) {
self.taskid = id;
self.section = section;
self.argidx = i;
}
#[inline]
pub fn set_taskid(&mut self, id:TaskId) {
self.taskid = id
}
}
impl<T> Default for CondAddr<T> {
fn default() -> Self {
Self::from((TaskId::NONE, Section::Input, ArgIdx::AINONE))
}
}
impl<T> From<(TaskId,Section,ArgIdx<T>)> for CondAddr<T> {
fn from((tid,section,argi): (TaskId,Section,ArgIdx<T>)) -> Self {
Self {
taskid: tid,
section,
argidx: argi,
}
}
}
impl<T> Debug for CondAddr<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"CondAddr<{typename}>{{{:?},{}({:?})}}",
&self.taskid,
if self.section == Section::Input {"Input"} else {"Output"},
self.argidx().i(),
typename=type_name::<T>())
}
}
#[derive(Clone, Copy, PartialEq)]
#[repr(transparent)]
pub struct TaskId(pub(crate) Option<NonZeroUsize>);
impl TaskId {
pub const NONE: Self = Self(None);
#[inline]
pub(crate) const fn new(id:usize)->Self {
Self(NonZeroUsize::new(id))
}
#[inline]
pub const fn as_usize(&self)->usize {
match self.0 {
Some(v) => v.get(),
None => 0,
}
}
}
impl From<usize> for TaskId {
fn from(id: usize) -> Self {
#[cfg(debug_assertions)]
if id == 0 {
panic!("TaskId cannot be zero");
}
#[cfg(not(debug_assertions))]
if crate::log::LEVEL as usize >= crate::log::Level::Warn as usize && id == 0 {
warn!("TaskId::new() the input id is zero, is not avaiable!");
}
Self::new(id)
}
}
impl std::fmt::Debug for TaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(taskid) = self.0 {
f.write_fmt(format_args!("TaskId({})",taskid.get()))
} else {
f.write_fmt(format_args!("TaskId(None)"))
}
}
}
#[test]
fn test_tid() {
let _tid = TaskId::from(3);
let _tid: TaskId = 3.into();
let _tid = TaskId::new(0);
}
#[derive(Copy, Clone, PartialEq)]
#[repr(transparent)]
pub struct ArgIdx<T>(pub(crate) u8,PhantomData<T>);
impl<T> ArgIdx<T> {
pub const AI0:ArgIdx<T> = ArgIdx(0,PhantomData);
pub const AI1:ArgIdx<T> = ArgIdx(1,PhantomData);
pub const AI2:ArgIdx<T> = ArgIdx(2,PhantomData);
pub const AI3:ArgIdx<T> = ArgIdx(3,PhantomData);
pub const AI4:ArgIdx<T> = ArgIdx(4,PhantomData);
pub const AI5:ArgIdx<T> = ArgIdx(5,PhantomData);
pub const AI6:ArgIdx<T> = ArgIdx(6,PhantomData);
pub const AI7:ArgIdx<T> = ArgIdx(7,PhantomData);
pub const AI8:ArgIdx<T> = ArgIdx(8,PhantomData);
pub const AINONE:ArgIdx<T> = ArgIdx(u8::MAX,PhantomData);
pub(crate) const fn const_new<const I:u8>() -> Self {
ArgIdx(I,PhantomData)
}
}
impl<T> ArgIdx<T> {
pub(crate) const fn i(&self)->u8 {
self.0
}
}
impl<T> From<u8> for ArgIdx<T> {
#[inline]
fn from(pi: u8) -> Self {
Self(pi,PhantomData)
}
}
impl<T> From<ArgIdx<T>> for u8 {
#[inline]
fn from(pi: ArgIdx<T>) -> Self {
pi.0
}
}
impl<T> Debug for ArgIdx<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Arg<{name}>({:?})", &self.0, name=type_name::<T>())
}
}
#[test]
fn test_argidx() {
let _ai = ArgIdx::<i8>::AI0; let _ai = ArgIdx::<i8>::from(2); let _ai: ArgIdx::<i8> = 2.into(); }
#[derive(PartialEq, Debug)]
pub enum Section {
Input,
Output,
}