new_direct -> unsafe new_unchecked

This commit is contained in:
Jörn-Michael Miehe 2025-07-10 11:00:35 +00:00
parent 2248d8fdd1
commit ab76563b83
3 changed files with 18 additions and 11 deletions

View file

@ -32,7 +32,8 @@ impl AsRef<[u8]> for Checked {
} }
impl Checked { impl Checked {
pub(super) fn new_direct(path: PathBuf, size: u64, hash: Option<String>) -> Self { /// create this directly, without any checks
pub(super) unsafe fn new_unchecked(path: PathBuf, size: u64, hash: Option<String>) -> Self {
Self { path, size, hash } Self { path, size, hash }
} }
@ -93,9 +94,7 @@ impl Checked {
) -> crate::Result<Uploading> { ) -> crate::Result<Uploading> {
let file_id = client.file_create(uri, alias_id, share_id, &self)?; let file_id = client.file_create(uri, alias_id, share_id, &self)?;
Ok(Uploading::new_direct( Ok(unsafe { Uploading::new_unchecked(self.path, self.size, self.hash, file_id) })
self.path, self.size, self.hash, file_id,
))
} }
} }
@ -160,8 +159,8 @@ mod tests {
"Checked", "Checked",
); );
// new_direct // new unchecked
let chk = Checked::new_direct(chk.path, chk.size, chk.hash); let chk = unsafe { Checked::new_unchecked(chk.path, chk.size, chk.hash) };
assert_eq!(chk.path, path); assert_eq!(chk.path, path);
assert_eq!(chk.size, size); assert_eq!(chk.size, size);
assert!(chk.hash.is_none()); assert!(chk.hash.is_none());

View file

@ -40,7 +40,12 @@ where
} }
impl<'t> Chunk<'t> { impl<'t> Chunk<'t> {
pub(super) fn new_direct(file_id: sharry::FileID, offset: u64, data: &'t [u8]) -> Self { /// create this directly, without any checks
pub(super) unsafe fn new_unchecked(
file_id: sharry::FileID,
offset: u64,
data: &'t [u8],
) -> Self {
Self { Self {
file_id, file_id,
offset, offset,
@ -83,9 +88,10 @@ mod tests {
#[test] #[test]
fn basic_tests() { fn basic_tests() {
// items from `DATA_LENGTHS_BAD` used as mock offsets
for (data, len, mock_offset) in cases_with(DATA_LENGTHS_BAD) { for (data, len, mock_offset) in cases_with(DATA_LENGTHS_BAD) {
let fid = sharry::FileID::default(); let fid = sharry::FileID::default();
let chunk = Chunk::new_direct(fid, mock_offset, data); let chunk = unsafe { Chunk::new_unchecked(fid, mock_offset, data) };
let repr_expect = format!( let repr_expect = format!(
"Chunk {{ file_id: {:?}, offset: {:?}, data.len(): {:?}, .. }}", "Chunk {{ file_id: {:?}, offset: {:?}, data.len(): {:?}, .. }}",

View file

@ -33,7 +33,8 @@ pub struct Uploading {
} }
impl Uploading { impl Uploading {
pub(super) fn new_direct( /// create this directly, without any checks
pub(super) unsafe fn new_unchecked(
path: PathBuf, path: PathBuf,
size: u64, size: u64,
hash: Option<String>, hash: Option<String>,
@ -93,7 +94,8 @@ impl Uploading {
)); ));
} }
let chunk = Chunk::new_direct(self.file_id.clone(), self.offset, &buf[..read_len]); let chunk =
unsafe { Chunk::new_unchecked(self.file_id.clone(), self.offset, &buf[..read_len]) };
self.previous_offset = Some(self.offset); self.previous_offset = Some(self.offset);
self.offset += chunk.get_length(); self.offset += chunk.get_length();
@ -117,7 +119,7 @@ impl Uploading {
/// ///
/// - consume self, returning as a `file::Checked` /// - consume self, returning as a `file::Checked`
pub fn stop(self) -> Checked { pub fn stop(self) -> Checked {
Checked::new_direct(self.path, self.size, self.hash) unsafe { Checked::new_unchecked(self.path, self.size, self.hash) }
} }
} }