Skip to content

Commit 0442ee2

Browse files
committed
add #[inline] attribute to many text::Reader and text::Builder methods
The idea here is to lower the likelihood of any performance degradation caused by the changes in the 0.18 release.
1 parent 10b9e31 commit 0442ee2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

capnp/src/text.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ impl<'a> core::fmt::Debug for Reader<'a> {
6767
}
6868

6969
impl<'a> From<&'a str> for Reader<'a> {
70+
#[inline]
7071
fn from(value: &'a str) -> Self {
7172
Self(value.as_bytes())
7273
}
7374
}
7475

7576
impl<'a> From<&'a [u8]> for Reader<'a> {
77+
#[inline]
7678
fn from(value: &'a [u8]) -> Self {
7779
Self(value)
7880
}
@@ -103,27 +105,32 @@ impl<'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
103105

104106
impl<'a> Reader<'a> {
105107
/// The string's length, in bytes.
108+
#[inline]
106109
pub fn len(&self) -> usize {
107110
self.as_bytes().len()
108111
}
109112

113+
#[inline]
110114
pub fn is_empty(&self) -> bool {
111115
self.len() == 0
112116
}
113117

118+
#[inline]
114119
pub fn as_bytes(self) -> &'a [u8] {
115120
let Self(d) = self;
116121
d
117122
}
118123

119124
/// Converts to a `str`, returning a error if the data contains invalid utf-8.
125+
#[inline]
120126
pub fn to_str(self) -> core::result::Result<&'a str, core::str::Utf8Error> {
121127
let Self(s) = self;
122128
str::from_utf8(s)
123129
}
124130

125131
#[cfg(feature = "alloc")]
126132
/// Converts to a `String`, returning a error if the data contains invalid utf-8.
133+
#[inline]
127134
pub fn to_string(self) -> core::result::Result<alloc::string::String, core::str::Utf8Error> {
128135
Ok(self.to_str()?.into())
129136
}
@@ -156,50 +163,60 @@ impl<'a> core::cmp::PartialEq<Builder<'a>> for &'a str {
156163
}
157164

158165
impl<'a> Builder<'a> {
166+
#[inline]
159167
pub fn new(bytes: &mut [u8]) -> Builder<'_> {
160168
Builder { bytes, pos: 0 }
161169
}
162170

171+
#[inline]
163172
pub fn with_pos(bytes: &mut [u8], pos: usize) -> Builder<'_> {
164173
Builder { bytes, pos }
165174
}
166175

167176
/// The string's length, in bytes.
177+
#[inline]
168178
pub fn len(&self) -> usize {
169179
self.bytes.len()
170180
}
171181

182+
#[inline]
172183
pub fn is_empty(&self) -> bool {
173184
self.len() == 0
174185
}
175186

187+
#[inline]
176188
pub fn as_bytes(self) -> &'a [u8] {
177189
self.bytes
178190
}
179191

180192
/// Converts to a `str`, returning a error if the data contains invalid utf-8.
193+
#[inline]
181194
pub fn to_str(self) -> core::result::Result<&'a str, core::str::Utf8Error> {
182195
str::from_utf8(self.bytes)
183196
}
184197

185198
#[cfg(feature = "alloc")]
186199
/// Converts to a `String`, returning a error if the data contains invalid utf-8.
200+
#[inline]
187201
pub fn to_string(self) -> core::result::Result<alloc::string::String, core::str::Utf8Error> {
188202
Ok(self.to_str()?.into())
189203
}
190204

205+
#[inline]
191206
pub fn as_bytes_mut(self) -> &'a mut [u8] {
192207
&mut self.bytes[..]
193208
}
194209

195210
/// Writes a single ascii character at position `pos` and increments `pos`.
211+
#[inline]
196212
pub fn push_ascii(&mut self, ascii: u8) {
197213
assert!(ascii < 128);
198214
self.bytes[self.pos] = ascii;
199215
self.pos += 1;
200216
}
201217

202218
/// Writes a string at position `pos` and increases `pos` a corresponding amount.
219+
#[inline]
203220
pub fn push_str(&mut self, string: &str) {
204221
let bytes = string.as_bytes();
205222
self.bytes[self.pos..(self.pos + bytes.len())].copy_from_slice(bytes);
@@ -214,17 +231,20 @@ impl<'a> Builder<'a> {
214231
self.pos = 0;
215232
}
216233

234+
#[inline]
217235
pub fn reborrow(&mut self) -> Builder<'_> {
218236
Builder {
219237
bytes: self.bytes,
220238
pos: self.pos,
221239
}
222240
}
223241

242+
#[inline]
224243
pub fn into_reader(self) -> Reader<'a> {
225244
Reader(self.bytes)
226245
}
227246

247+
#[inline]
228248
pub fn reborrow_as_reader(&self) -> Reader<'_> {
229249
Reader(self.bytes)
230250
}

0 commit comments

Comments
 (0)