Skip to main content

pit_c_generic/
lib.rs

1#![no_std]
2pub mod __ {
3    pub use core;
4}
5use core::{fmt::Display, marker::PhantomData};
6extern crate alloc;
7// #[repr(transparent)]
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
9pub struct C<T, Kind> {
10    pub value: T,
11    pub kind: Kind,
12}
13#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
14pub struct PureC<T> {
15    pub cx: T,
16}
17#[macro_export]
18macro_rules! c_disp {
19    ($(<$($g:ident $(: $gp:path)?),*>)? [$k:ty] $t:ty => |$self:pat_param, $fmt:pat_param, $kind:pat_param|$a:expr) => {
20        impl $(<$($g : $($gp)?),*>)? $crate::__::core::fmt::Display for $crate::C<$t, $k> {
21            fn fmt(
22                &self,
23                f: &mut $crate::__::core::fmt::Formatter<'_>,
24            ) -> $crate::__::core::result::Result<(), $crate::__::core::fmt::Error> {
25                match &self.value {
26                    $self => match f {
27                        $fmt => match &self.kind{
28                            $kind => $a,
29                        },
30                    },
31                }
32            }
33        }
34        impl<'a $(,$($g : $($gp)?),*)?> $crate::__::core::fmt::Display for $crate::C<&'a $t, $k> {
35            fn fmt(
36                &self,
37                f: &mut $crate::__::core::fmt::Formatter<'_>,
38            ) -> $crate::__::core::result::Result<(), $crate::__::core::fmt::Error> {
39                match &*self.value {
40                    $self => match f {
41                        $fmt => match &self.kind{
42                            $kind => $a,
43                        },
44                    },
45                }
46            }
47        }
48    };
49}
50c_disp!(<T: Display>[PureC<T>]pit_core::Arg => |this,f,kind|match this{
51    pit_core::Arg::I32 => write!(f,"uint32_t"),
52    pit_core::Arg::I64 => write!(f,"uint64_t"),
53    pit_core::Arg::F32 => write!(f,"float"),
54    pit_core::Arg::F64 => write!(f,"double"),
55    pit_core::Arg::Resource { ty, nullable, take, ann } => match ty{
56        pit_core::ResTy::None => write!(f,"Any_T"),
57        pit_core::ResTy::Of(a) => write!(f,"{}{}_t",&kind.cx,hex::encode(a)),
58        pit_core::ResTy::This => write!(f,"CUR"),
59        _ => todo!(),
60    },
61    _ => todo!(),
62});
63c_disp!(<T: Display>[PureC<T>]pit_core::Sig => |this,f,kind|{
64    write!(f,"vfunc(struct{{")?;
65    for (i,r) in this.rets.iter().enumerate(){
66        write!(f,"{} r{i}",C{value:r,kind:PureC{cx:&kind.cx}})?;
67    }
68    write!(f,"}},METH,VSelf,")?;
69    for (i,p) in this.params.iter().enumerate(){
70        write!(f,"{} p{i}",C{value:p,kind:PureC{cx:&kind.cx}})?;
71    };
72    Ok(())
73});
74c_disp!(<T: Display>[PureC<T>]pit_core::Interface => |this,f,kind|{
75    let cx = &kind.cx;
76    // write!(f,"#define CUR {cx}{}_t\n",hex::encode(this.rid()))?;
77    for (k,m) in this.methods.iter(){
78        // write!(f,"#define METH {k}\n")?;
79        write!(f,"#define {cx}{}_t_IFACE_{k}(CUR,METH) {}\n",hex::encode(this.rid()),C{value:m,kind:PureC{cx:&kind.cx}})?;
80    }
81    write!(f,"#define {cx}{}_t_IFACE ",hex::encode(this.rid()))?;
82    for k in this.methods.keys(){
83        write!(f,"{cx}{}_t_IFACE_{k}({cx}{0}_t,{k}) ",hex::encode(this.rid()))?;
84    }
85    write!(f,"\n")?;
86    write!(f,"interface({cx}{}_t)\n",hex::encode(this.rid()))?;
87    Ok(())
88});