Skip to content

Commit 7bdc378

Browse files
committed
chore: minor internal refactoring
1 parent f9329bb commit 7bdc378

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

Diff for: src/bytes.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::ops::{Bound, Deref, DerefMut, Range, RangeBounds};
1111
use core::ptr;
1212

1313
use raw::borrowed::Borrowed;
14-
use raw::{Inline, RawSplit, RawSplitMut, Tag, Union};
14+
use raw::{Inline, Split, SplitMut, Tag, Union};
1515

1616
use self::raw::try_range_of;
1717
pub use self::raw::HipByt;
@@ -136,9 +136,9 @@ where
136136
#[must_use]
137137
pub const fn len(&self) -> usize {
138138
match self.split() {
139-
RawSplit::Inline(inline) => inline.len(),
140-
RawSplit::Allocated(heap) => heap.len(),
141-
RawSplit::Borrowed(borrowed) => borrowed.len(),
139+
Split::Inline(inline) => inline.len(),
140+
Split::Allocated(heap) => heap.len(),
141+
Split::Borrowed(borrowed) => borrowed.len(),
142142
}
143143
}
144144

@@ -228,8 +228,8 @@ where
228228
/// ```
229229
pub const fn into_borrowed(self) -> Result<&'borrow [u8], Self> {
230230
match self.split() {
231-
RawSplit::Allocated(_) | RawSplit::Inline(_) => Err(self),
232-
RawSplit::Borrowed(borrowed) => {
231+
Split::Allocated(_) | Split::Inline(_) => Err(self),
232+
Split::Borrowed(borrowed) => {
233233
let result = borrowed.as_slice();
234234
core::mem::forget(self); // not needed
235235
Ok(result)
@@ -257,8 +257,8 @@ where
257257
#[must_use]
258258
pub const fn as_borrowed(&self) -> Option<&'borrow [u8]> {
259259
match self.split() {
260-
RawSplit::Allocated(_) | RawSplit::Inline(_) => None,
261-
RawSplit::Borrowed(borrowed) => Some(borrowed.as_slice()),
260+
Split::Allocated(_) | Split::Inline(_) => None,
261+
Split::Borrowed(borrowed) => Some(borrowed.as_slice()),
262262
}
263263
}
264264

@@ -318,9 +318,9 @@ where
318318
#[must_use]
319319
pub fn capacity(&self) -> usize {
320320
match self.split() {
321-
RawSplit::Inline(_) => Self::inline_capacity(),
322-
RawSplit::Borrowed(borrowed) => borrowed.len(), // provide something to simplify the API
323-
RawSplit::Allocated(allocated) => allocated.capacity(),
321+
Split::Inline(_) => Self::inline_capacity(),
322+
Split::Borrowed(borrowed) => borrowed.len(), // provide something to simplify the API
323+
Split::Allocated(allocated) => allocated.capacity(),
324324
}
325325
}
326326

@@ -733,9 +733,9 @@ where
733733
#[inline]
734734
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
735735
match self.split_mut() {
736-
RawSplitMut::Borrowed(_) => &mut [],
737-
RawSplitMut::Inline(inline) => inline.spare_capacity_mut(),
738-
RawSplitMut::Allocated(allocated) => allocated.spare_capacity_mut(),
736+
SplitMut::Borrowed(_) => &mut [],
737+
SplitMut::Inline(inline) => inline.spare_capacity_mut(),
738+
SplitMut::Allocated(allocated) => allocated.spare_capacity_mut(),
739739
}
740740
}
741741

@@ -751,11 +751,11 @@ where
751751
/// * The vector should not be shared.
752752
pub unsafe fn set_len(&mut self, new_len: usize) {
753753
match self.split_mut() {
754-
RawSplitMut::Borrowed(borrowed) => unsafe {
754+
SplitMut::Borrowed(borrowed) => unsafe {
755755
borrowed.set_len(new_len);
756756
},
757-
RawSplitMut::Inline(inline) => unsafe { inline.set_len(new_len) },
758-
RawSplitMut::Allocated(allocated) => unsafe { allocated.set_len(new_len) },
757+
SplitMut::Inline(inline) => unsafe { inline.set_len(new_len) },
758+
SplitMut::Allocated(allocated) => unsafe { allocated.set_len(new_len) },
759759
}
760760
}
761761

Diff for: src/bytes/raw.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub enum Tag {
158158
}
159159

160160
/// Helper enum to split this raw byte string into its possible representation.
161-
pub enum RawSplit<'a, 'borrow, B: Backend> {
161+
pub enum Split<'a, 'borrow, B: Backend> {
162162
/// Inline representation
163163
Inline(&'a Inline),
164164
/// Allocated and shared representation
@@ -168,7 +168,7 @@ pub enum RawSplit<'a, 'borrow, B: Backend> {
168168
}
169169

170170
/// Helper enum to split this raw byte string into its possible representation mutably.
171-
pub enum RawSplitMut<'a, 'borrow, B: Backend> {
171+
pub enum SplitMut<'a, 'borrow, B: Backend> {
172172
/// Inline representation
173173
Inline(&'a mut Inline),
174174
/// Allocated and shared representation
@@ -239,42 +239,42 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
239239

240240
/// Splits this raw into its possible representation.
241241
#[inline]
242-
pub(super) const fn split(&self) -> RawSplit<'_, 'borrow, B> {
242+
pub(super) const fn split(&self) -> Split<'_, 'borrow, B> {
243243
let tag = self.tag();
244244
let union = self.union();
245245
match tag {
246246
Tag::Inline => {
247247
// SAFETY: representation checked
248-
RawSplit::Inline(unsafe { &union.inline })
248+
Split::Inline(unsafe { &union.inline })
249249
}
250250
Tag::Borrowed => {
251251
// SAFETY: representation checked
252-
RawSplit::Borrowed(unsafe { &union.borrowed })
252+
Split::Borrowed(unsafe { &union.borrowed })
253253
}
254254
Tag::Allocated => {
255255
// SAFETY: representation checked
256-
RawSplit::Allocated(unsafe { &union.allocated })
256+
Split::Allocated(unsafe { &union.allocated })
257257
}
258258
}
259259
}
260260

261261
/// Splits this raw into its possible representation.
262262
#[inline]
263-
pub(super) fn split_mut(&mut self) -> RawSplitMut<'_, 'borrow, B> {
263+
pub(super) fn split_mut(&mut self) -> SplitMut<'_, 'borrow, B> {
264264
let tag = self.tag();
265265
let union = self.union_mut();
266266
match tag {
267267
Tag::Inline => {
268268
// SAFETY: representation checked
269-
RawSplitMut::Inline(unsafe { &mut union.inline })
269+
SplitMut::Inline(unsafe { &mut union.inline })
270270
}
271271
Tag::Borrowed => {
272272
// SAFETY: representation checked
273-
RawSplitMut::Borrowed(unsafe { &mut union.borrowed })
273+
SplitMut::Borrowed(unsafe { &mut union.borrowed })
274274
}
275275
Tag::Allocated => {
276276
// SAFETY: representation checked
277-
RawSplitMut::Allocated(unsafe { &mut union.allocated })
277+
SplitMut::Allocated(unsafe { &mut union.allocated })
278278
}
279279
}
280280
}
@@ -296,7 +296,7 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
296296
/// # Safety
297297
///
298298
/// The input slice's length MUST be at most `INLINE_CAPACITY`.
299-
pub(super) unsafe fn inline_unchecked(bytes: &[u8]) -> Self {
299+
pub(super) const unsafe fn inline_unchecked(bytes: &[u8]) -> Self {
300300
// SAFETY: see function precondition
301301
let inline = unsafe { Inline::new_unchecked(bytes) };
302302
Self::from_inline(inline)
@@ -349,9 +349,9 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
349349
#[must_use]
350350
pub const fn as_slice(&self) -> &[u8] {
351351
match self.split() {
352-
RawSplit::Inline(inline) => inline.as_slice(),
353-
RawSplit::Allocated(heap) => heap.as_slice(),
354-
RawSplit::Borrowed(borrowed) => borrowed.as_slice(),
352+
Split::Inline(inline) => inline.as_slice(),
353+
Split::Allocated(heap) => heap.as_slice(),
354+
Split::Borrowed(borrowed) => borrowed.as_slice(),
355355
}
356356
}
357357

@@ -360,9 +360,9 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
360360
#[must_use]
361361
pub const fn as_ptr(&self) -> *const u8 {
362362
match self.split() {
363-
RawSplit::Inline(inline) => inline.as_ptr(),
364-
RawSplit::Allocated(heap) => heap.as_ptr(),
365-
RawSplit::Borrowed(borrowed) => borrowed.as_ptr(),
363+
Split::Inline(inline) => inline.as_ptr(),
364+
Split::Allocated(heap) => heap.as_ptr(),
365+
Split::Borrowed(borrowed) => borrowed.as_ptr(),
366366
}
367367
}
368368

@@ -379,13 +379,13 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
379379
debug_assert!(range.end <= self.len());
380380

381381
let result = match self.split() {
382-
RawSplit::Inline(inline) => {
382+
Split::Inline(inline) => {
383383
// SAFETY: by `slice_unchecked` safety precondition and `split`
384384
// range must be of a length <= self.len() <= `INLINE_CAPACITY`
385385
unsafe { Self::inline_unchecked(&inline.as_slice()[range]) }
386386
}
387-
RawSplit::Borrowed(borrowed) => Self::borrowed(&borrowed.as_slice()[range]),
388-
RawSplit::Allocated(allocated) => {
387+
Split::Borrowed(borrowed) => Self::borrowed(&borrowed.as_slice()[range]),
388+
Split::Allocated(allocated) => {
389389
// normalize to inline if possible
390390
if range.len() <= INLINE_CAPACITY {
391391
// SAFETY: length is checked above
@@ -424,18 +424,18 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
424424
}
425425

426426
let result = match self.split() {
427-
RawSplit::Inline(_) => {
427+
Split::Inline(_) => {
428428
// SAFETY: by the function precondition and the test above
429429
// slice.len() <= self.len() <= INLINE_CAPACITY
430430
unsafe { Self::inline_unchecked(slice) }
431431
}
432-
RawSplit::Borrowed(_) => {
432+
Split::Borrowed(_) => {
433433
// SAFETY: by the function precondition and the type invariant
434434
// slice must have at least the same dynamic lifetime
435435
let sl: &'borrow [u8] = unsafe { transmute(slice) };
436436
Self::borrowed(sl)
437437
}
438-
RawSplit::Allocated(allocated) => {
438+
Split::Allocated(allocated) => {
439439
// normalize to inline if possible
440440
if slice.len() <= INLINE_CAPACITY {
441441
// SAFETY: length checked above
@@ -473,9 +473,9 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
473473
#[must_use]
474474
pub fn as_mut_slice(&mut self) -> Option<&mut [u8]> {
475475
match self.split_mut() {
476-
RawSplitMut::Inline(inline) => Some(inline.as_mut_slice()),
477-
RawSplitMut::Allocated(allocated) => allocated.as_mut_slice(),
478-
RawSplitMut::Borrowed(_) => None,
476+
SplitMut::Inline(inline) => Some(inline.as_mut_slice()),
477+
SplitMut::Allocated(allocated) => allocated.as_mut_slice(),
478+
SplitMut::Borrowed(_) => None,
479479
}
480480
}
481481

@@ -487,9 +487,9 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
487487
#[inline]
488488
pub(super) unsafe fn as_mut_slice_unchecked(&mut self) -> &mut [u8] {
489489
match self.split_mut() {
490-
RawSplitMut::Inline(inline) => inline.as_mut_slice(),
491-
RawSplitMut::Allocated(allocated) => unsafe { allocated.as_mut_slice_unchecked() },
492-
RawSplitMut::Borrowed(_) => {
490+
SplitMut::Inline(inline) => inline.as_mut_slice(),
491+
SplitMut::Allocated(allocated) => unsafe { allocated.as_mut_slice_unchecked() },
492+
SplitMut::Borrowed(_) => {
493493
#[cfg(debug_assertions)]
494494
{
495495
panic!("mutable slice of borrowed string");
@@ -531,7 +531,7 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> {
531531
#[inline]
532532
pub(super) fn take_allocated(&mut self) -> Option<Allocated<B>> {
533533
match self.split() {
534-
RawSplit::Allocated(&allocated) => {
534+
Split::Allocated(&allocated) => {
535535
// Takes a copy of allocated
536536

537537
// replace `self` one by an empty raw
@@ -622,9 +622,9 @@ impl<B: Backend> Clone for HipByt<'_, B> {
622622
fn clone(&self) -> Self {
623623
// Duplicates this `Raw` increasing the ref count if needed.
624624
match self.split() {
625-
RawSplit::Inline(&inline) => Self::from_inline(inline),
626-
RawSplit::Borrowed(&borrowed) => Self::from_borrowed(borrowed),
627-
RawSplit::Allocated(allocated) => {
625+
Split::Inline(&inline) => Self::from_inline(inline),
626+
Split::Borrowed(&borrowed) => Self::from_borrowed(borrowed),
627+
Split::Allocated(allocated) => {
628628
let clone = allocated.explicit_clone();
629629
Self::from_allocated(clone)
630630
}

0 commit comments

Comments
 (0)