Skip to content

Commit 3ad9297

Browse files
committed
Replace Deref for BString by u8
1 parent 756eee2 commit 3ad9297

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

gix-object/src/tree/mod.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use bstr::ByteSlice;
2+
13
use crate::{
24
bstr::{BStr, BString},
35
tree, Tree, TreeRef,
46
};
5-
use std::cell::RefCell;
67
use std::cmp::Ordering;
8+
use std::{cell::RefCell, u8};
79

810
///
911
pub mod editor;
@@ -336,13 +338,43 @@ pub struct RepositoryPathPuf {
336338
inner: BString,
337339
}
338340

341+
impl RepositoryPathPuf {
342+
fn len(&self) -> usize {
343+
self.inner.len()
344+
}
345+
346+
fn get(&self, index: usize) -> std::option::Option<&u8> {
347+
self.inner.get(index)
348+
}
349+
350+
fn find_byte(&self, byte: u8) -> std::option::Option<usize> {
351+
self.inner.find_byte(byte)
352+
}
353+
354+
/// TODO
355+
pub fn to_bstring(&self) -> BString {
356+
self.inner.clone()
357+
}
358+
}
359+
339360
impl std::fmt::Display for RepositoryPathPuf {
340361
#[inline]
341362
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
342363
self.inner.fmt(f)
343364
}
344365
}
345366

367+
impl std::ops::Index<std::ops::RangeTo<usize>> for RepositoryPathPuf {
368+
type Output = BStr;
369+
370+
#[inline]
371+
fn index(&self, r: std::ops::RangeTo<usize>) -> &BStr {
372+
use bstr::ByteSlice;
373+
374+
BStr::new(&self.inner.as_bytes()[..r.end])
375+
}
376+
}
377+
346378
impl From<&str> for RepositoryPathPuf {
347379
fn from(value: &str) -> Self {
348380
Self { inner: value.into() }
@@ -362,10 +394,10 @@ impl From<BString> for RepositoryPathPuf {
362394
}
363395

364396
impl std::ops::Deref for RepositoryPathPuf {
365-
type Target = BString;
397+
type Target = [u8];
366398

367399
#[inline]
368-
fn deref(&self) -> &BString {
400+
fn deref(&self) -> &[u8] {
369401
&self.inner
370402
}
371403
}

gix-object/src/tree/write.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ impl crate::WriteTo for Tree {
4040
out.write_all(mode.as_bytes(&mut buf))?;
4141
out.write_all(SPACE)?;
4242

43-
let name: &BString = filename;
44-
4543
if filename.find_byte(0).is_some() {
4644
return Err(Error::NullbyteInFilename {
47-
name: (*name).to_owned(),
45+
name: filename.to_bstring(),
4846
}
4947
.into());
5048
}

gix/src/object/tree/editor.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,15 @@ fn write_cursor<'repo>(cursor: &mut Cursor<'_, 'repo>) -> Result<Id<'repo>, writ
287287
.then_some(gix_validate::path::component::Mode::Symlink),
288288
cursor.validate,
289289
)
290-
.map_err(|err| {
291-
let filename: &BString = &entry.filename;
292-
293-
write::Error::InvalidFilename {
294-
filename: filename.clone(),
295-
kind: entry.mode.into(),
296-
id: entry.oid,
297-
source: err,
298-
}
290+
.map_err(|err| write::Error::InvalidFilename {
291+
filename: entry.filename.to_bstring(),
292+
kind: entry.mode.into(),
293+
id: entry.oid,
294+
source: err,
299295
})?;
300296
if !entry.mode.is_commit() && !cursor.repo.has_object(entry.oid) {
301-
let filename: &BString = &entry.filename;
302-
303297
return Err(write::Error::MissingObject {
304-
filename: filename.clone(),
298+
filename: entry.filename.to_bstring(),
305299
kind: entry.mode.into(),
306300
id: entry.oid,
307301
});

0 commit comments

Comments
 (0)