Skip to content

Commit e8dec22

Browse files
committed
Don't unnecessarily Copy encodings
1 parent d20a973 commit e8dec22

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

objc2-encode/src/encoding.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn equivalent_to(enc1: &Encoding<'_>, enc2: &Encoding<'_>, level: NestingLevel)
218218
// should compare equal, but we don't bother since in practice a plain
219219
// `Unknown` will never appear.
220220
use Helper::*;
221-
match (Helper::new(*enc1), Helper::new(*enc2)) {
221+
match (Helper::new(enc1), Helper::new(enc2)) {
222222
(Primitive(p1), Primitive(p2)) => p1 == p2,
223223
(BitField(b1, type1), BitField(b2, type2)) => {
224224
b1 == b2 && equivalent_to(type1, type2, level.bitfield())
@@ -252,7 +252,11 @@ fn equivalent_to(enc1: &Encoding<'_>, enc2: &Encoding<'_>, level: NestingLevel)
252252
}
253253
}
254254

255-
fn display_fmt(this: Encoding<'_>, f: &mut fmt::Formatter<'_>, level: NestingLevel) -> fmt::Result {
255+
fn display_fmt(
256+
this: &Encoding<'_>,
257+
f: &mut fmt::Formatter<'_>,
258+
level: NestingLevel,
259+
) -> fmt::Result {
256260
use Helper::*;
257261
match Helper::new(this) {
258262
Primitive(primitive) => f.write_str(primitive.to_str()),
@@ -262,12 +266,12 @@ fn display_fmt(this: Encoding<'_>, f: &mut fmt::Formatter<'_>, level: NestingLev
262266
}
263267
Indirection(kind, t) => {
264268
write!(f, "{}", kind.prefix())?;
265-
display_fmt(*t, f, level.indirection(kind))
269+
display_fmt(t, f, level.indirection(kind))
266270
}
267271
Array(len, item) => {
268272
write!(f, "[")?;
269273
write!(f, "{}", len)?;
270-
display_fmt(*item, f, level.array())?;
274+
display_fmt(item, f, level.array())?;
271275
write!(f, "]")
272276
}
273277
Container(kind, name, fields) => {
@@ -276,7 +280,7 @@ fn display_fmt(this: Encoding<'_>, f: &mut fmt::Formatter<'_>, level: NestingLev
276280
if let Some(level) = level.container() {
277281
write!(f, "=")?;
278282
for field in fields {
279-
display_fmt(*field, f, level)?;
283+
display_fmt(field, f, level)?;
280284
}
281285
}
282286
write!(f, "{}", kind.end())
@@ -292,7 +296,7 @@ fn display_fmt(this: Encoding<'_>, f: &mut fmt::Formatter<'_>, level: NestingLev
292296
/// Objective-C compilers.
293297
impl fmt::Display for Encoding<'_> {
294298
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295-
display_fmt(*self, f, NestingLevel::new())
299+
display_fmt(self, f, NestingLevel::new())
296300
}
297301
}
298302

@@ -375,7 +379,7 @@ mod tests {
375379
)*
376380

377381
// Check static str
378-
const STATIC_ENCODING_DATA: [u8; static_encoding_str_len(E, NestingLevel::new())] = static_encoding_str_array(E, NestingLevel::new());
382+
const STATIC_ENCODING_DATA: [u8; static_encoding_str_len(&E, NestingLevel::new())] = static_encoding_str_array(&E, NestingLevel::new());
379383
const STATIC_ENCODING_STR: &str = unsafe { core::str::from_utf8_unchecked(&STATIC_ENCODING_DATA) };
380384
assert_eq!(STATIC_ENCODING_STR, $string);
381385
}

objc2-encode/src/helper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub(crate) enum Helper<'a> {
167167
}
168168

169169
impl<'a> Helper<'a> {
170-
pub(crate) const fn new(encoding: Encoding<'a>) -> Self {
170+
pub(crate) const fn new(encoding: &Encoding<'a>) -> Self {
171171
use Encoding::*;
172172
match encoding {
173173
Char => Self::Primitive(Primitive::Char),
@@ -194,10 +194,10 @@ impl<'a> Helper<'a> {
194194
Class => Self::Primitive(Primitive::Class),
195195
Sel => Self::Primitive(Primitive::Sel),
196196
Unknown => Self::Primitive(Primitive::Unknown),
197-
BitField(b, t) => Self::BitField(b, t),
197+
BitField(b, t) => Self::BitField(*b, t),
198198
Pointer(t) => Self::Indirection(IndirectionKind::Pointer, t),
199199
Atomic(t) => Self::Indirection(IndirectionKind::Atomic, t),
200-
Array(len, item) => Self::Array(len, item),
200+
Array(len, item) => Self::Array(*len, item),
201201
Struct(name, fields) => Self::Container(ContainerKind::Struct, name, fields),
202202
Union(name, members) => Self::Container(ContainerKind::Union, name, members),
203203
}

objc2-encode/src/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) fn rm_enc_prefix<'a>(
2020
level: NestingLevel,
2121
) -> Option<&'a str> {
2222
use Helper::*;
23-
match Helper::new(*enc) {
23+
match Helper::new(enc) {
2424
Primitive(primitive) => s.strip_prefix(primitive.to_str()),
2525
BitField(b, _type) => {
2626
// TODO: Use the type on GNUStep (nesting level?)

objc2-encode/src/static_str.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) const fn static_int_str_array<const RES: usize>(mut n: u128) -> [u8;
3838
rev
3939
}
4040

41-
pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>, level: NestingLevel) -> usize {
41+
pub(crate) const fn static_encoding_str_len(encoding: &Encoding<'_>, level: NestingLevel) -> usize {
4242
use Helper::*;
4343

4444
match Helper::new(encoding) {
@@ -47,8 +47,8 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>, level: Nesti
4747
// TODO: Use the type on GNUStep (nesting level?)
4848
1 + static_int_str_len(b as u128)
4949
}
50-
Indirection(kind, &t) => 1 + static_encoding_str_len(t, level.indirection(kind)),
51-
Array(len, &item) => {
50+
Indirection(kind, t) => 1 + static_encoding_str_len(t, level.indirection(kind)),
51+
Array(len, item) => {
5252
1 + static_int_str_len(len as u128) + static_encoding_str_len(item, level.array()) + 1
5353
}
5454
Container(_, name, items) => {
@@ -57,7 +57,7 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>, level: Nesti
5757
res += 1;
5858
let mut i = 0;
5959
while i < items.len() {
60-
res += static_encoding_str_len(items[i], level);
60+
res += static_encoding_str_len(&items[i], level);
6161
i += 1;
6262
}
6363
}
@@ -67,7 +67,7 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>, level: Nesti
6767
}
6868

6969
pub(crate) const fn static_encoding_str_array<const LEN: usize>(
70-
encoding: Encoding<'_>,
70+
encoding: &Encoding<'_>,
7171
level: NestingLevel,
7272
) -> [u8; LEN] {
7373
use Helper::*;
@@ -84,7 +84,7 @@ pub(crate) const fn static_encoding_str_array<const LEN: usize>(
8484
i += 1;
8585
}
8686
}
87-
BitField(b, &_type) => {
87+
BitField(b, _type) => {
8888
// TODO: Use the type on GNUStep (nesting level?)
8989
res[res_i] = b'b';
9090
res_i += 1;
@@ -98,7 +98,7 @@ pub(crate) const fn static_encoding_str_array<const LEN: usize>(
9898
i += 1;
9999
}
100100
}
101-
Indirection(kind, &t) => {
101+
Indirection(kind, t) => {
102102
res[res_i] = kind.prefix_byte();
103103
res_i += 1;
104104

@@ -111,7 +111,7 @@ pub(crate) const fn static_encoding_str_array<const LEN: usize>(
111111
i += 1;
112112
}
113113
}
114-
Array(len, &item) => {
114+
Array(len, item) => {
115115
let mut res_i = 0;
116116

117117
res[res_i] = b'[';
@@ -158,10 +158,10 @@ pub(crate) const fn static_encoding_str_array<const LEN: usize>(
158158
let mut items_i = 0;
159159
while items_i < items.len() {
160160
// We use LEN even though it creates an oversized array
161-
let field_res = static_encoding_str_array::<LEN>(items[items_i], level);
161+
let field_res = static_encoding_str_array::<LEN>(&items[items_i], level);
162162

163163
let mut item_res_i = 0;
164-
while item_res_i < static_encoding_str_len(items[items_i], level) {
164+
while item_res_i < static_encoding_str_len(&items[items_i], level) {
165165
res[res_i] = field_res[item_res_i];
166166
res_i += 1;
167167
item_res_i += 1;

0 commit comments

Comments
 (0)