Skip to content

Commit e05884f

Browse files
committed
cleanup
Signed-off-by: Heinz N. Gies <heinz@licenser.net>
1 parent 293adfc commit e05884f

File tree

8 files changed

+97
-148
lines changed

8 files changed

+97
-148
lines changed

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,16 +1126,14 @@ impl AlignedBuf {
11261126
/// Creates a new buffer that is aligned with the simd register size
11271127
#[must_use]
11281128
pub fn with_capacity(capacity: usize) -> Self {
1129-
let layout = match Layout::from_size_align(capacity, SIMDJSON_PADDING) {
1130-
Ok(layout) => layout,
1131-
Err(_) => Self::capacity_overflow(),
1129+
let Ok(layout) = Layout::from_size_align(capacity, SIMDJSON_PADDING) else {
1130+
Self::capacity_overflow()
11321131
};
11331132
if mem::size_of::<usize>() < 8 && capacity > isize::MAX as usize {
11341133
Self::capacity_overflow()
11351134
}
1136-
let inner = match unsafe { NonNull::new(alloc(layout)) } {
1137-
Some(ptr) => ptr,
1138-
None => handle_alloc_error(layout),
1135+
let Some(inner) = NonNull::new(unsafe { alloc(layout) }) else {
1136+
handle_alloc_error(layout)
11391137
};
11401138
Self {
11411139
layout,

src/numberparse/correct.rs

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ use crate::safer_unchecked::GetSaferUnchecked;
1212
use crate::StaticNode;
1313
use crate::{Deserializer, ErrorType, Result};
1414

15+
macro_rules! err {
16+
($idx:ident, $num:expr) => {
17+
return Err(Error::new_c($idx, $num as char, ErrorType::InvalidNumber))
18+
};
19+
}
20+
1521
macro_rules! get {
1622
($buf:ident, $idx:expr) => {
1723
unsafe { *$buf.get_kinda_unchecked($idx as usize) }
@@ -23,11 +29,7 @@ macro_rules! check_overflow {
2329
if $overflowed {
2430
#[cfg(not(feature = "big-int-as-float"))]
2531
{
26-
return Err(Error::new_c(
27-
$idx,
28-
get!($buf, $idx) as char,
29-
ErrorType::InvalidNumber,
30-
));
32+
err!($idx, get!($buf, $idx));
3133
}
3234
#[cfg(feature = "big-int-as-float")]
3335
{
@@ -61,31 +63,19 @@ impl<'de> Deserializer<'de> {
6163
if negative {
6264
idx += 1;
6365
if !is_integer(get!(buf, idx)) {
64-
return Err(Error::new_c(
65-
idx,
66-
get!(buf, idx) as char,
67-
ErrorType::InvalidNumber,
68-
));
66+
err!(idx, get!(buf, idx));
6967
}
7068
}
7169
let mut start = idx;
7270
let mut num: u64 = 0;
7371
if get!(buf, idx) == b'0' {
7472
idx += 1;
7573
if is_not_structural_or_whitespace_or_exponent_or_decimal(get!(buf, idx)) {
76-
return Err(Error::new_c(
77-
idx,
78-
get!(buf, idx) as char,
79-
ErrorType::InvalidNumber,
80-
));
74+
err!(idx, get!(buf, idx));
8175
}
8276
} else {
8377
if !is_integer(get!(buf, idx)) {
84-
return Err(Error::new_c(
85-
idx,
86-
get!(buf, idx) as char,
87-
ErrorType::InvalidNumber,
88-
));
78+
err!(idx, get!(buf, idx));
8979
}
9080
num = u64::from(get!(buf, idx) - b'0');
9181
idx += 1;
@@ -108,11 +98,7 @@ impl<'de> Deserializer<'de> {
10898
.wrapping_add(u64::from(get!(buf, idx) - b'0'));
10999
idx += 1;
110100
} else {
111-
return Err(Error::new_c(
112-
idx,
113-
get!(buf, idx) as char,
114-
ErrorType::InvalidNumber,
115-
));
101+
err!(idx, get!(buf, idx));
116102
}
117103

118104
#[cfg(feature = "swar-number-parsing")]
@@ -155,11 +141,7 @@ impl<'de> Deserializer<'de> {
155141
}
156142
}
157143
if !is_integer(get!(buf, idx)) {
158-
return Err(Error::new_c(
159-
idx,
160-
get!(buf, idx) as char,
161-
ErrorType::InvalidNumber,
162-
));
144+
err!(idx, get!(buf, idx));
163145
}
164146
let mut exp_number = i64::from(get!(buf, idx) - b'0');
165147
idx += 1;
@@ -173,11 +155,7 @@ impl<'de> Deserializer<'de> {
173155
}
174156
while is_integer(get!(buf, idx)) {
175157
if exp_number > 0x0001_0000_0000 {
176-
return Err(Error::new_c(
177-
idx,
178-
get!(buf, idx) as char,
179-
ErrorType::InvalidNumber,
180-
));
158+
err!(idx, get!(buf, idx));
181159
}
182160
exp_number = 10 * exp_number + i64::from(get!(buf, idx) - b'0');
183161
idx += 1;
@@ -202,11 +180,7 @@ impl<'de> Deserializer<'de> {
202180
}
203181
}
204182
if is_structural_or_whitespace(get!(buf, idx)) == 0 {
205-
return Err(Error::new_c(
206-
idx,
207-
get!(buf, idx) as char,
208-
ErrorType::InvalidNumber,
209-
));
183+
err!(idx, get!(buf, idx));
210184
}
211185
Ok(f64_from_parts(
212186
!negative,
@@ -224,13 +198,11 @@ impl<'de> Deserializer<'de> {
224198
ErrorType::InvalidNumber,
225199
))
226200
} else {
227-
let res = if negative {
201+
Ok(if negative {
228202
StaticNode::I64(unsafe { static_cast_i64!(num.wrapping_neg()) })
229-
// -(num as i64)
230203
} else {
231204
StaticNode::U64(num)
232-
};
233-
Ok(res)
205+
})
234206
}
235207
}
236208
}
@@ -362,11 +334,9 @@ fn f64_from_parts(
362334
} else {
363335
f *= get!(POW10, exponent);
364336
}
365-
let res = StaticNode::F64(if positive { f } else { -f });
366-
Ok(res)
337+
Ok(StaticNode::F64(if positive { f } else { -f }))
367338
} else if significand == 0 {
368-
let res = StaticNode::F64(if positive { 0.0 } else { -0.0 });
369-
Ok(res)
339+
Ok(StaticNode::F64(if positive { 0.0 } else { -0.0 }))
370340
} else if (-325..=308).contains(&exponent) {
371341
let (factor_mantissa, factor_exponent) = get!(POW10_COMPONENTS, exponent + 325);
372342
let mut leading_zeroes = u64::from(significand.leading_zeros());
@@ -421,8 +391,7 @@ fn f64_from_parts(
421391
ErrorType::InvalidNumber,
422392
))
423393
} else {
424-
let res = StaticNode::F64(res);
425-
Ok(res)
394+
Ok(StaticNode::F64(res))
426395
}
427396
} else {
428397
let res = f64_from_parts_slow(slice, offset)?;
@@ -438,21 +407,12 @@ fn f64_from_parts_slow(slice: &[u8], offset: usize) -> Result<StaticNode> {
438407
match unsafe { std::str::from_utf8_unchecked(slice).parse::<f64>() } {
439408
Ok(val) => {
440409
if val.is_infinite() {
441-
return Err(Error::new_c(
442-
offset,
443-
get!(slice, 0) as char,
444-
ErrorType::InvalidNumber,
445-
));
410+
err!(offset, get!(slice, 0));
446411
}
447-
448412
Ok(StaticNode::F64(val))
449413
}
450414
Err(_) => {
451-
return Err(Error::new_c(
452-
offset,
453-
get!(slice, offset) as char,
454-
ErrorType::InvalidNumber,
455-
))
415+
err!(offset, get!(slice, offset));
456416
}
457417
}
458418
}

src/serde/value/borrowed/de.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,8 @@ impl<'de> de::Deserializer<'de> for Value<'de> {
8080
let (variant, value) = match self {
8181
Value::Object(value) => {
8282
let mut iter = value.into_iter();
83-
let (variant, value) = match iter.next() {
84-
Some(v) => v,
85-
None => {
86-
return Err(crate::Deserializer::error(ErrorType::Eof));
87-
}
83+
let Some((variant, value)) = iter.next() else {
84+
return Err(crate::Deserializer::error(ErrorType::Eof));
8885
};
8986
// enums are encoded in json as maps with a single key:value pair
9087
if iter.next().is_some() {
@@ -685,11 +682,8 @@ impl<'de> de::Deserializer<'de> for &'de Value<'de> {
685682
let (variant, value) = match self {
686683
Value::Object(value) => {
687684
let mut iter = value.iter();
688-
let (variant, value) = match iter.next() {
689-
Some(v) => v,
690-
None => {
691-
return Err(crate::Deserializer::error(ErrorType::Eof));
692-
}
685+
let Some((variant, value)) = iter.next() else {
686+
return Err(crate::Deserializer::error(ErrorType::Eof));
693687
};
694688
// enums are encoded in json as maps with a single key:value pair
695689
if iter.next().is_some() {

src/serde/value/owned/de.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ impl<'de> de::Deserializer<'de> for Value {
6868
let (variant, value) = match self {
6969
Value::Object(value) => {
7070
let mut iter = value.into_iter();
71-
let (variant, value) = match iter.next() {
72-
Some(v) => v,
73-
None => {
74-
return Err(crate::Deserializer::error(ErrorType::Eof));
75-
}
71+
let Some((variant, value)) = iter.next() else {
72+
return Err(crate::Deserializer::error(ErrorType::Eof));
7673
};
7774
// enums are encoded in json as maps with a single key:value pair
7875
if iter.next().is_some() {

src/value/borrowed/cmp.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ impl<'v> PartialEq<bool> for Value<'v> {
4949
#[cfg_attr(not(feature = "no-inline"), inline)]
5050
#[must_use]
5151
fn eq(&self, other: &bool) -> bool {
52-
self.as_bool().map(|t| t.eq(other)).unwrap_or_default()
52+
self.as_bool().is_some_and(|t| t.eq(other))
5353
}
5454
}
5555

5656
impl<'v> PartialEq<str> for Value<'v> {
5757
#[cfg_attr(not(feature = "no-inline"), inline)]
5858
#[must_use]
5959
fn eq(&self, other: &str) -> bool {
60-
self.as_str().map(|t| t.eq(other)).unwrap_or_default()
60+
self.as_str().is_some_and(|t| t.eq(other))
6161
}
6262
}
6363

@@ -73,111 +73,111 @@ impl<'v> PartialEq<String> for Value<'v> {
7373
#[cfg_attr(not(feature = "no-inline"), inline)]
7474
#[must_use]
7575
fn eq(&self, other: &String) -> bool {
76-
self.as_str().map(|t| t.eq(other)).unwrap_or_default()
76+
self.as_str().is_some_and(|t| t.eq(other))
7777
}
7878
}
7979

8080
impl<'v> PartialEq<i8> for Value<'v> {
8181
#[cfg_attr(not(feature = "no-inline"), inline)]
8282
#[must_use]
8383
fn eq(&self, other: &i8) -> bool {
84-
self.as_i8().map(|t| t.eq(other)).unwrap_or_default()
84+
self.as_i8().is_some_and(|t| t.eq(other))
8585
}
8686
}
8787

8888
impl<'v> PartialEq<i16> for Value<'v> {
8989
#[cfg_attr(not(feature = "no-inline"), inline)]
9090
#[must_use]
9191
fn eq(&self, other: &i16) -> bool {
92-
self.as_i16().map(|t| t.eq(other)).unwrap_or_default()
92+
self.as_i16().is_some_and(|t| t.eq(other))
9393
}
9494
}
9595

9696
impl<'v> PartialEq<i32> for Value<'v> {
9797
#[cfg_attr(not(feature = "no-inline"), inline)]
9898
#[must_use]
9999
fn eq(&self, other: &i32) -> bool {
100-
self.as_i32().map(|t| t.eq(other)).unwrap_or_default()
100+
self.as_i32().is_some_and(|t| t.eq(other))
101101
}
102102
}
103103

104104
impl<'v> PartialEq<i64> for Value<'v> {
105105
#[cfg_attr(not(feature = "no-inline"), inline)]
106106
#[must_use]
107107
fn eq(&self, other: &i64) -> bool {
108-
self.as_i64().map(|t| t.eq(other)).unwrap_or_default()
108+
self.as_i64().is_some_and(|t| t.eq(other))
109109
}
110110
}
111111

112112
impl<'v> PartialEq<i128> for Value<'v> {
113113
#[cfg_attr(not(feature = "no-inline"), inline)]
114114
#[must_use]
115115
fn eq(&self, other: &i128) -> bool {
116-
self.as_i128().map(|t| t.eq(other)).unwrap_or_default()
116+
self.as_i128().is_some_and(|t| t.eq(other))
117117
}
118118
}
119119

120120
impl<'v> PartialEq<u8> for Value<'v> {
121121
#[cfg_attr(not(feature = "no-inline"), inline)]
122122
#[must_use]
123123
fn eq(&self, other: &u8) -> bool {
124-
self.as_u8().map(|t| t.eq(other)).unwrap_or_default()
124+
self.as_u8().is_some_and(|t| t.eq(other))
125125
}
126126
}
127127

128128
impl<'v> PartialEq<u16> for Value<'v> {
129129
#[cfg_attr(not(feature = "no-inline"), inline)]
130130
#[must_use]
131131
fn eq(&self, other: &u16) -> bool {
132-
self.as_u16().map(|t| t.eq(other)).unwrap_or_default()
132+
self.as_u16().is_some_and(|t| t.eq(other))
133133
}
134134
}
135135

136136
impl<'v> PartialEq<u32> for Value<'v> {
137137
#[cfg_attr(not(feature = "no-inline"), inline)]
138138
#[must_use]
139139
fn eq(&self, other: &u32) -> bool {
140-
self.as_u32().map(|t| t.eq(other)).unwrap_or_default()
140+
self.as_u32().is_some_and(|t| t.eq(other))
141141
}
142142
}
143143

144144
impl<'v> PartialEq<u64> for Value<'v> {
145145
#[cfg_attr(not(feature = "no-inline"), inline)]
146146
#[must_use]
147147
fn eq(&self, other: &u64) -> bool {
148-
self.as_u64().map(|t| t.eq(other)).unwrap_or_default()
148+
self.as_u64().is_some_and(|t| t.eq(other))
149149
}
150150
}
151151

152152
impl<'v> PartialEq<usize> for Value<'v> {
153153
#[cfg_attr(not(feature = "no-inline"), inline)]
154154
#[must_use]
155155
fn eq(&self, other: &usize) -> bool {
156-
self.as_usize().map(|t| t.eq(other)).unwrap_or_default()
156+
self.as_usize().is_some_and(|t| t.eq(other))
157157
}
158158
}
159159

160160
impl<'v> PartialEq<u128> for Value<'v> {
161161
#[cfg_attr(not(feature = "no-inline"), inline)]
162162
#[must_use]
163163
fn eq(&self, other: &u128) -> bool {
164-
self.as_u128().map(|t| t.eq(other)).unwrap_or_default()
164+
self.as_u128().is_some_and(|t| t.eq(other))
165165
}
166166
}
167167

168168
impl<'v> PartialEq<f32> for Value<'v> {
169169
#[cfg_attr(not(feature = "no-inline"), inline)]
170170
#[must_use]
171171
fn eq(&self, other: &f32) -> bool {
172-
self.as_f32().map(|t| t.eq(other)).unwrap_or_default()
172+
self.as_f32().is_some_and(|t| t.eq(other))
173173
}
174174
}
175175

176176
impl<'v> PartialEq<f64> for Value<'v> {
177177
#[cfg_attr(not(feature = "no-inline"), inline)]
178178
#[must_use]
179179
fn eq(&self, other: &f64) -> bool {
180-
self.as_f64().map(|t| t.eq(other)).unwrap_or_default()
180+
self.as_f64().is_some_and(|t| t.eq(other))
181181
}
182182
}
183183

@@ -188,7 +188,7 @@ where
188188
#[cfg_attr(not(feature = "no-inline"), inline)]
189189
#[must_use]
190190
fn eq(&self, other: &&[T]) -> bool {
191-
self.as_array().map(|t| t.eq(other)).unwrap_or_default()
191+
self.as_array().is_some_and(|t| t.eq(other))
192192
}
193193
}
194194

0 commit comments

Comments
 (0)