-
Notifications
You must be signed in to change notification settings - Fork 10
/
pargument.rs
158 lines (143 loc) · 4.38 KB
/
pargument.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![allow(clippy::wrong_self_convention)]
use crate::{
formatting::{Formatting, FormattingFlags},
wrapper_types::PWrapper,
};
#[doc(hidden)]
/// The uniform representation for every argument of the concatcp macro.
pub struct PArgument {
pub elem: PVariant,
pub fmt_len: usize,
pub fmt: Formatting,
pub fmt_flags: FormattingFlags,
}
impl PArgument {
/// Calculates the length of the string after adding up all the PArguments
pub const fn calc_len(mut args: &[PArgument]) -> usize {
let mut sum = 0;
while let [curr, rem @ ..] = args {
args = rem;
sum += curr.fmt_len;
}
sum
}
}
#[doc(hidden)]
pub enum PVariant {
Str(&'static str),
Int(Integer),
}
#[derive(Debug, Copy, Clone)]
pub struct Integer {
pub is_negative: bool,
pub unsigned: u128,
pub mask: &'static u128, // A mask which disables the bits that weren't in the original number
}
#[doc(hidden)]
pub struct PConvWrapper<T>(pub T);
macro_rules! pconvwrapper_impls {
( $( ($Signed:ty, $Unsigned:ty) )* ) => (
pconvwrapper_impls!{
@inner to_pargument_display, compute_display_len, Formatting::Display;
$(($Signed, $Unsigned))*
}
pconvwrapper_impls!{
@inner to_pargument_debug, compute_debug_len, Formatting::Debug;
$(($Signed, $Unsigned))*
}
$(
#[doc(hidden)]
impl PConvWrapper<$Signed>{
pub const fn to_integer(self)->Integer{
Integer{
is_negative: self.0 < 0,
unsigned: PWrapper(self.0).unsigned_abs() as u128,
mask: &(((!(0 as $Signed)) as $Unsigned) as u128),
}
}
}
#[doc(hidden)]
impl PConvWrapper<$Unsigned>{
pub const fn to_integer(self)->Integer{
Integer{
is_negative: false,
unsigned: self.0 as u128,
mask: &((!(0 as $Unsigned)) as u128),
}
}
}
)*
);
(@inner
$method:ident,
$called:ident,
$formatting:expr;
$( ($Signed:ty, $Unsigned:ty) )*
) => (
$(
#[doc(hidden)]
impl PConvWrapper<$Signed> {
pub const fn $method(self, fmt_flags: FormattingFlags)->PArgument{
PArgument {
fmt_len: $crate::pmr::PWrapper(self.0).$called(fmt_flags),
fmt: $formatting,
fmt_flags,
elem: PVariant::Int(self.to_integer()),
}
}
}
#[doc(hidden)]
impl PConvWrapper<$Unsigned> {
pub const fn $method(self, fmt_flags: FormattingFlags)->PArgument{
PArgument {
fmt_len: $crate::pmr::PWrapper(self.0).$called(fmt_flags),
fmt: $formatting,
fmt_flags,
elem: PVariant::Int(self.to_integer()),
}
}
}
)*
);
}
pconvwrapper_impls! {
(i8, u8)
(i16, u16)
(i32, u32)
(i64, u64)
(i128, u128)
(isize, usize)
}
#[doc(hidden)]
impl PConvWrapper<bool> {
#[inline]
pub const fn to_pargument_display(self, _: FormattingFlags) -> PArgument {
PConvWrapper(if self.0 { "true" } else { "false" })
.to_pargument_display(FormattingFlags::DEFAULT)
}
#[inline]
pub const fn to_pargument_debug(self, fmt_flags: FormattingFlags) -> PArgument {
self.to_pargument_display(fmt_flags)
}
}
#[doc(hidden)]
impl PConvWrapper<&'static str> {
#[inline]
pub const fn to_pargument_display(self, fmt_flags: FormattingFlags) -> PArgument {
PArgument {
fmt_len: PWrapper(self.0).compute_display_len(fmt_flags),
fmt_flags,
fmt: Formatting::Display,
elem: PVariant::Str(self.0),
}
}
#[inline]
pub const fn to_pargument_debug(self, fmt_flags: FormattingFlags) -> PArgument {
PArgument {
fmt_len: PWrapper(self.0).compute_debug_len(fmt_flags),
fmt_flags,
fmt: Formatting::Debug,
elem: PVariant::Str(self.0),
}
}
}