marker_api/ast/ty/
sequence_ty.rs

1use crate::{
2    ast::expr::ConstExpr,
3    ffi::{FfiOption, FfiSlice},
4};
5
6use super::{CommonSynTyData, TyKind};
7
8/// The syntactic representation of a tuple type like [`()`](prim@tuple) or [`(T, U)`](prim@tuple)
9#[repr(C)]
10#[derive(Debug)]
11pub struct TupleTy<'ast> {
12    data: CommonSynTyData<'ast>,
13    types: FfiSlice<'ast, TyKind<'ast>>,
14}
15
16#[cfg(feature = "driver-api")]
17impl<'ast> TupleTy<'ast> {
18    pub fn new(data: CommonSynTyData<'ast>, types: &'ast [TyKind<'ast>]) -> Self {
19        Self {
20            data,
21            types: types.into(),
22        }
23    }
24}
25
26super::impl_ty_data!(TupleTy<'ast>, Tuple);
27
28impl<'ast> TupleTy<'ast> {
29    pub fn types(&self) -> &[TyKind<'ast>] {
30        self.types.as_slice()
31    }
32}
33
34impl<'ast> std::fmt::Display for TupleTy<'ast> {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        let mut f = f.debug_tuple("");
37
38        for entry in self.types.as_slice() {
39            f.field(entry);
40        }
41
42        f.finish()
43    }
44}
45
46/// The syntactic representation of a variable length slice like [`[T]`](prim@slice)
47#[repr(C)]
48#[derive(Debug)]
49pub struct SliceTy<'ast> {
50    data: CommonSynTyData<'ast>,
51    inner_ty: TyKind<'ast>,
52}
53
54#[cfg(feature = "driver-api")]
55impl<'ast> SliceTy<'ast> {
56    pub fn new(data: CommonSynTyData<'ast>, inner_ty: TyKind<'ast>) -> Self {
57        Self { data, inner_ty }
58    }
59}
60
61super::impl_ty_data!(SliceTy<'ast>, Slice);
62
63impl<'ast> SliceTy<'ast> {
64    pub fn inner_ty(&self) -> TyKind<'ast> {
65        self.inner_ty
66    }
67}
68
69impl<'ast> std::fmt::Display for SliceTy<'ast> {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        f.debug_list().entries(std::iter::once(self.inner_ty())).finish()
72    }
73}
74
75/// The syntactic representation of an array with a known size like: [`[T; N]`](prim@array)
76#[repr(C)]
77#[derive(Debug)]
78pub struct ArrayTy<'ast> {
79    data: CommonSynTyData<'ast>,
80    inner_ty: TyKind<'ast>,
81    // FIXME(xFrednet): This might need to change, if a syntax like `[1; _]` is
82    // ever supported, as proposed in https://github.com/rust-lang/rust/issues/85077
83    len: FfiOption<ConstExpr<'ast>>,
84}
85
86super::impl_ty_data!(ArrayTy<'ast>, Array);
87
88impl<'ast> ArrayTy<'ast> {
89    pub fn inner_ty(&self) -> TyKind<'ast> {
90        self.inner_ty
91    }
92
93    pub fn len(&self) -> Option<&ConstExpr<'ast>> {
94        self.len.get()
95    }
96}
97
98#[cfg(feature = "driver-api")]
99impl<'ast> ArrayTy<'ast> {
100    pub fn new(data: CommonSynTyData<'ast>, inner_ty: TyKind<'ast>, len: Option<ConstExpr<'ast>>) -> Self {
101        Self {
102            data,
103            inner_ty,
104            len: len.into(),
105        }
106    }
107}
108
109impl<'ast> std::fmt::Display for ArrayTy<'ast> {
110    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111        // FIXME: Add length expression
112        f.debug_list().entries(std::iter::once(self.inner_ty())).finish()
113    }
114}