Skip to content

Commit

Permalink
Merge pull request #38 from earthstar-project/day-ify
Browse files Browse the repository at this point in the history
Make willow-data-model + meadowcap crates no longer require nightly channel
  • Loading branch information
sgwilym authored Aug 9, 2024
2 parents 76dd377 + 9c80df8 commit b0c5381
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 89 deletions.
41 changes: 4 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion data-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dev = ["dep:arbitrary"]
[dependencies]
either = "1.10.0"
arbitrary = { version = "1.0.2", features = ["derive"], optional = true }
ufotofu = "0.3.0"
ufotofu = { version = "0.4.2", features = ["std"] }
bytes = "1.6.0"
syncify = "0.1.0"

Expand Down
2 changes: 1 addition & 1 deletion data-model/src/encoding/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::error::Error;
use core::{fmt::Display, fmt::Formatter, num::TryFromIntError};
use either::Either;
use std::error::Error;
use ufotofu::common::errors::OverwriteFullSliceError;

/// Everything that can go wrong when decoding a value.
Expand Down
17 changes: 9 additions & 8 deletions data-model/src/encoding/relativity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(super) mod encoding {
#[syncify_replace(use crate::encoding::bytes::encoding_sync::produce_byte;)]
use crate::encoding::bytes::encoding::produce_byte;

use core::mem::{size_of, MaybeUninit};
use core::mem::size_of;

use crate::{
encoding::{
Expand Down Expand Up @@ -121,12 +121,13 @@ pub(super) mod encoding {
// Copy the raw path data of the prefix into the scratch buffer.
unsafe {
// safe because we just copied the accumulated component lengths for the first `lcp_component_count` components.
MaybeUninit::copy_from_slice(
buf.path_data_until_as_mut(lcp_component_count),
&reference.raw_buf()[size_of::<usize>() * (reference.get_component_count() + 1)
..size_of::<usize>() * (reference.get_component_count() + 1)
+ prefix.get_path_length()],
);
buf.path_data_until_as_mut(lcp_component_count)
.copy_from_slice(
&reference.raw_buf()[size_of::<usize>()
* (reference.get_component_count() + 1)
..size_of::<usize>() * (reference.get_component_count() + 1)
+ prefix.get_path_length()],
);
}

let remaining_component_count: usize =
Expand All @@ -152,7 +153,7 @@ pub(super) mod encoding {

// Decode the component itself into the scratch buffer.
producer
.bulk_overwrite_full_slice_uninit(unsafe {
.bulk_overwrite_full_slice(unsafe {
// Safe because we called set_component_Accumulated_length for all j <= i
buf.path_data_as_mut(i)
})
Expand Down
33 changes: 14 additions & 19 deletions data-model/src/encoding/shared_buffers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Shared buffers to be reused across many decoding operations.
use core::mem::MaybeUninit;

use bytes::BytesMut;

use crate::path::Path;
Expand All @@ -10,15 +8,15 @@ use crate::path::Path;
#[derive(Debug)]
pub(crate) struct ScratchSpacePathDecoding<const MCC: usize, const MPL: usize> {
// The i-th usize holds the total lengths of the first i components.
component_accumulated_lengths: [MaybeUninit<usize>; MCC],
path_data: [MaybeUninit<u8>; MPL],
component_accumulated_lengths: [usize; MCC],
path_data: [u8; MPL],
}

impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
pub fn new() -> Self {
ScratchSpacePathDecoding {
component_accumulated_lengths: MaybeUninit::uninit_array(),
path_data: MaybeUninit::uninit_array(),
component_accumulated_lengths: [0; MCC],
path_data: [0; MPL],
}
}

Expand All @@ -28,29 +26,27 @@ impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
component_accumulated_length: usize,
i: usize,
) {
MaybeUninit::write(
&mut self.component_accumulated_lengths[i],
component_accumulated_length,
);
self.component_accumulated_lengths[i] = component_accumulated_length;
}

/// # Safety
///
/// UB if length of slice is greater than `size_of::<usize>() * MCC`.
pub unsafe fn set_many_component_accumulated_lengths_from_ne(&mut self, lengths: &[u8]) {
let slice: &mut [MaybeUninit<u8>] = core::slice::from_raw_parts_mut(
let slice: &mut [u8] = core::slice::from_raw_parts_mut(
self.component_accumulated_lengths[..lengths.len() / size_of::<usize>()].as_mut_ptr()
as *mut MaybeUninit<u8>,
as *mut u8,
lengths.len(),
);
MaybeUninit::copy_from_slice(slice, lengths);

slice.copy_from_slice(lengths);
}

/// # Safety
///
/// Memory must have been initialised with a prior call to set_component_accumulated_length for the same `i`.
unsafe fn get_component_accumulated_length(&self, i: usize) -> usize {
MaybeUninit::assume_init(self.component_accumulated_lengths[i])
self.component_accumulated_lengths[i]
}

/// Return a slice of the accumulated component lengths up to but excluding the `i`-th component, encoded as native-endian u8s.
Expand All @@ -60,8 +56,7 @@ impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
/// Memory must have been initialised with prior call to set_component_accumulated_length for all `j <= i`
pub unsafe fn get_accumumulated_component_lengths(&self, i: usize) -> &[u8] {
core::slice::from_raw_parts(
MaybeUninit::slice_assume_init_ref(&self.component_accumulated_lengths[..i]).as_ptr()
as *const u8,
self.component_accumulated_lengths[..i].as_ptr() as *const u8,
i * size_of::<usize>(),
)
}
Expand All @@ -71,7 +66,7 @@ impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
/// # Safety
///
/// Accumulated component lengths for `i` and `i - 1` must have been set (only for `i` if `i == 0`).
pub unsafe fn path_data_as_mut(&mut self, i: usize) -> &mut [MaybeUninit<u8>] {
pub unsafe fn path_data_as_mut(&mut self, i: usize) -> &mut [u8] {
let start = if i == 0 {
0
} else {
Expand All @@ -86,7 +81,7 @@ impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
/// # Safety
///
/// Accumulated component lengths for `i - 1` must have been set (unless `i == 0`).
pub unsafe fn path_data_until_as_mut(&mut self, i: usize) -> &mut [MaybeUninit<u8>] {
pub unsafe fn path_data_until_as_mut(&mut self, i: usize) -> &mut [u8] {
let end = self.get_component_accumulated_length(i - 1);
&mut self.path_data[0..end]
}
Expand All @@ -103,7 +98,7 @@ impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
} else {
self.get_component_accumulated_length(i - 1)
};
return MaybeUninit::slice_assume_init_ref(&self.path_data[..end]);
&self.path_data[..end]
}

/// Copy the data from this struct into a new Path of `i` components.
Expand Down
9 changes: 0 additions & 9 deletions data-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@
//! - `PD` - The type used for [`PayloadDigest`](https://willowprotocol.org/specs/data-model/index.html#PayloadDigest) (willowprotocol.org), must implement the [`PayloadDigest`] trait.
//! - `AT` - The type used for [`AuthorisationToken`](https://willowprotocol.org/specs/data-model/index.html#AuthorisationToken) (willowprotocol.org), must implement the [`AuthorisationToken`] trait.
#![feature(
new_uninit,
async_fn_traits,
debug_closure_helpers,
maybe_uninit_uninit_array,
maybe_uninit_write_slice,
maybe_uninit_slice
)]

pub mod encoding;
mod entry;
pub use entry::*;
Expand Down
4 changes: 2 additions & 2 deletions data-model/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl core::fmt::Display for InvalidPathError {
}
}

impl core::error::Error for InvalidPathError {}
impl std::error::Error for InvalidPathError {}

/// An immutable Willow [path](https://willowprotocol.org/specs/data-model/index.html#Path). Thread-safe, cheap to clone, cheap to take prefixes of, expensive to append to.
///
Expand Down Expand Up @@ -846,7 +846,7 @@ mod encoding {

// Decode the component itself into the scratch buffer.
producer
.bulk_overwrite_full_slice_uninit(unsafe {
.bulk_overwrite_full_slice(unsafe {
// Safe because we called set_component_Accumulated_length for all j <= i
buf.path_data_as_mut(i)
})
Expand Down
4 changes: 2 additions & 2 deletions earthstar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
either = "1.10.0"
willow-data-model = { path = "../data-model" }
arbitrary = { version = "1.0.2", features = ["derive"]}
ufotofu = "0.3.0"
ufotofu = { version = "0.4.2", features = ["std"] }

[lints]
workspace = true
workspace = true
8 changes: 4 additions & 4 deletions earthstar/src/cinn25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Decodable
Producer: BulkProducer<Item = u8>,
{
if MIN_LENGTH == MAX_LENGTH {
let mut shortname_box = Box::new_uninit_slice(MIN_LENGTH);
let mut shortname_box = vec![0; MIN_LENGTH].into_boxed_slice();

let shortname_bytes = producer
.bulk_overwrite_full_slice_uninit(shortname_box.as_mut())
producer
.bulk_overwrite_full_slice(shortname_box.as_mut())
.await?;

let mut underlying_slice = [0u8; 32];
Expand All @@ -149,7 +149,7 @@ impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Decodable
.await?;

return Ok(Self {
shortname: Shortname(shortname_bytes.to_vec()),
shortname: Shortname(shortname_box.into()),
underlying: underlying_slice,
});
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cargo-fuzz = true
workspace = true

[dependencies]
ufotofu = { version = "0.3.0", features=["dev"]}
ufotofu = { version = "0.4.2", features = ["std", "dev"] }
smol = "2.0.0"
arbitrary = { version = "1.0.2", features = ["derive"] }
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
Expand Down
8 changes: 4 additions & 4 deletions fuzz/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where

let mut new_vec = Vec::new();

new_vec.extend_from_slice(consumer.as_ref());
new_vec.extend_from_slice(consumer.consumed());

// THis should eventually be a testproducer, when we are able to initialise one with some known data.
let mut producer = FromBoxedSlice::from_vec(new_vec);
Expand All @@ -53,7 +53,7 @@ where

item.encode(&mut consumer).await.unwrap();

let encoded = consumer.as_ref().as_slice();
let encoded = consumer.as_ref();

assert_eq!(encoded, &data[0..producer.get_offset()]);
}
Expand Down Expand Up @@ -133,7 +133,7 @@ pub async fn relative_encoding_roundtrip<T, R, C>(

let mut new_vec = Vec::new();

new_vec.extend_from_slice(consumer.as_ref());
new_vec.extend_from_slice(consumer.consumed());

// THis should eventually be a testproducer, when we are able to initialise one with some known data.
let mut producer = FromBoxedSlice::from_vec(new_vec);
Expand Down Expand Up @@ -164,7 +164,7 @@ where
.await
.unwrap();

let encoded = consumer.as_ref().as_slice();
let encoded = consumer.as_ref();

assert_eq!(encoded, &data[0..producer.get_offset()]);
}
Expand Down
2 changes: 1 addition & 1 deletion meadowcap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dev = ["dep:arbitrary"]

[dependencies]
signature = "2.2.0"
ufotofu = "0.3.0"
ufotofu = { version = "0.4.2", features = ["std"] }
arbitrary = { version = "1.0.2", features = ["derive"], optional = true }
either = "1.13.0"
syncify = "0.1.0"
Expand Down

0 comments on commit b0c5381

Please sign in to comment.