use super::*;
use crate::{
abi_stability::ConstGeneric,
};
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, StableAbi)]
#[sabi(unsafe_sabi_opaque_fields)]
pub enum ReprAttr{
OptionNonZero,
Primitive,
C,
CAndInt(DiscriminantRepr),
Transparent,
Int(DiscriminantRepr),
#[doc(hidden)]
Packed{
alignment_power_of_two:u8,
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone,Eq,PartialEq, StableAbi)]
#[sabi(unsafe_sabi_opaque_fields)]
pub struct ModPath(NulStr<'static>);
impl ModPath{
pub const NO_PATH:Self=ModPath(nul_str!("<no path>"));
pub const PRELUDE:Self=ModPath(nul_str!("<prelude>"));
pub const fn inside(path:NulStr<'static>)->Self{
ModPath(path)
}
}
impl Display for ModPath{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0,f)
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq,StableAbi)]
#[sabi(unsafe_sabi_opaque_fields)]
pub struct CompGenericParams {
lifetime: NulStr<'static>,
types: StartLen,
consts: StartLen,
lifetime_count:u8,
}
impl CompGenericParams{
pub const fn new(
lifetime: NulStr<'static>,
lifetime_count:u8,
types:StartLen,
consts:StartLen,
)->Self{
Self{
lifetime,
lifetime_count,
types,
consts,
}
}
pub fn expand(self,shared_vars:&'static SharedVars)->GenericParams{
GenericParams{
lifetime: self.lifetime,
types: &shared_vars.type_layouts()[self.types.to_range()],
consts: &shared_vars.constants()[self.consts.to_range()],
lifetime_count:self.lifetime_count,
}
}
}
#[derive(Copy,Clone,PartialEq,Eq)]
pub struct GenericParams{
pub(super) lifetime: NulStr<'static>,
pub(super) types: &'static [TypeLayoutCtor],
pub(super) consts: &'static [ConstGeneric],
pub(super) lifetime_count:u8,
}
impl GenericParams {
pub fn is_empty(&self) -> bool {
self.lifetime.to_str().is_empty() && self.types.is_empty() && self.consts.is_empty()
}
pub fn lifetimes(&self)-> impl Iterator<Item=&'static str>+Clone+Send+Sync+'static {
self.lifetime.to_str().split(',').filter(|x| !x.is_empty() )
}
pub fn lifetime_count(&self)->usize{
self.lifetime_count as usize
}
pub fn type_params(&self)->&'static [TypeLayoutCtor]{
self.types
}
pub fn const_params(&self)->&'static [ConstGeneric]{
self.consts
}
}
impl Display for GenericParams {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt("<", f)?;
let post_iter = |i: usize, len: usize, f: &mut Formatter<'_>| -> fmt::Result {
if i + 1 < len {
fmt::Display::fmt(", ", f)?;
}
Ok(())
};
for (i, param) in self.lifetimes().enumerate() {
fmt::Display::fmt(param, &mut *f)?;
post_iter(i, self.lifetime_count(), &mut *f)?;
}
for (i, param) in self.types.iter().cloned().enumerate() {
fmt::Debug::fmt(¶m.get().full_type(), &mut *f)?;
post_iter(i, self.types.len(), &mut *f)?;
}
for (i, param) in self.consts.iter().enumerate() {
fmt::Debug::fmt(param, &mut *f)?;
post_iter(i, self.consts.len(), &mut *f)?;
}
fmt::Display::fmt(">", f)?;
Ok(())
}
}
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq,StableAbi)]
#[sabi(unsafe_sabi_opaque_fields)]
pub enum TLPrimitive{
U8,
I8,
U16,
I16,
U32,
I32,
U64,
I64,
Usize,
Isize,
Bool,
SharedRef,
MutRef,
ConstPtr,
MutPtr,
Array{
len:usize,
},
}
#[derive(Copy,Clone,PartialEq,Eq)]
pub struct FmtFullType {
pub(super) name:&'static str,
pub(super) generics:GenericParams,
pub(super) primitive:Option<TLPrimitive>,
pub(super) utypeid:UTypeId,
}
impl FmtFullType{
pub fn name(&self)->&'static str{
self.name
}
pub fn generics(&self)->GenericParams{
self.generics
}
}
#[repr(u8)]
#[derive(Copy,Clone,Debug,Eq,PartialEq,StableAbi)]
#[sabi(unsafe_sabi_opaque_fields)]
pub enum TLFieldOrFunction{
Field(TLField),
Function(TLFunction),
}
impl From<TLField> for TLFieldOrFunction{
fn from(x:TLField)->Self{
TLFieldOrFunction::Field(x)
}
}
impl From<TLFunction> for TLFieldOrFunction{
fn from(x:TLFunction)->Self{
TLFieldOrFunction::Function(x)
}
}
impl Display for TLFieldOrFunction{
fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
match self {
TLFieldOrFunction::Field(x)=>Display::fmt(x,f),
TLFieldOrFunction::Function(x)=>Display::fmt(x,f),
}
}
}
impl TLFieldOrFunction{
pub fn formatted_layout(&self)->String{
match self {
TLFieldOrFunction::Field(x)=>x.layout().to_string(),
TLFieldOrFunction::Function(x)=>x.to_string(),
}
}
}