Skip to content

Commit 2b8f377

Browse files
committed
Replace Deref for BString by u8
1 parent 7676dc2 commit 2b8f377

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

gix-object/src/tree/mod.rs

+35-3
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;
@@ -262,13 +264,43 @@ pub struct RepositoryPathPuf {
262264
inner: BString,
263265
}
264266

267+
impl RepositoryPathPuf {
268+
fn len(&self) -> usize {
269+
self.inner.len()
270+
}
271+
272+
fn get(&self, index: usize) -> std::option::Option<&u8> {
273+
self.inner.get(index)
274+
}
275+
276+
fn find_byte(&self, byte: u8) -> std::option::Option<usize> {
277+
self.inner.find_byte(byte)
278+
}
279+
280+
/// TODO
281+
pub fn to_bstring(&self) -> BString {
282+
self.inner.clone()
283+
}
284+
}
285+
265286
impl std::fmt::Display for RepositoryPathPuf {
266287
#[inline]
267288
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
268289
self.inner.fmt(f)
269290
}
270291
}
271292

293+
impl std::ops::Index<std::ops::RangeTo<usize>> for RepositoryPathPuf {
294+
type Output = BStr;
295+
296+
#[inline]
297+
fn index(&self, r: std::ops::RangeTo<usize>) -> &BStr {
298+
use bstr::ByteSlice;
299+
300+
BStr::new(&self.inner.as_bytes()[..r.end])
301+
}
302+
}
303+
272304
impl From<&str> for RepositoryPathPuf {
273305
fn from(value: &str) -> Self {
274306
Self { inner: value.into() }
@@ -288,10 +320,10 @@ impl From<BString> for RepositoryPathPuf {
288320
}
289321

290322
impl std::ops::Deref for RepositoryPathPuf {
291-
type Target = BString;
323+
type Target = [u8];
292324

293325
#[inline]
294-
fn deref(&self) -> &BString {
326+
fn deref(&self) -> &[u8] {
295327
&self.inner
296328
}
297329
}

gix-object/src/tree/write.rs

+1-3
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

+6-12
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)