Commit 6b6e523d authored by Hero Bird's avatar Hero Bird
Browse files

[pdsl_core] Rejigged cells, chunks and value with respect to their new_unchecked constructor

parent fe3ac781
......@@ -50,7 +50,7 @@ impl RawCell {
///
/// This is unsafe since it does not check if the associated
/// contract storage does not alias with other accesses.
pub unsafe fn new_unchecked(key: Key) -> Self {
unsafe fn new_unchecked(key: Key) -> Self {
Self{
key: key,
non_clone: NonCloneMarker::default()
......@@ -67,10 +67,7 @@ impl RawCell {
where
A: Allocator
{
Self{
key: alloc.alloc(1),
non_clone: Default::default(),
}
Self::new_unchecked(alloc.alloc(1))
}
}
......
......@@ -16,7 +16,6 @@
use crate::{
storage::{
Key,
cell::TypedCell,
Allocator,
},
......@@ -151,19 +150,6 @@ impl<T> parity_codec::Decode for SyncCell<T> {
}
impl<T> SyncCell<T> {
/// Creates a new copy cell for the given key.
///
/// # Safety
///
/// This is unsafe since it does not check if the associated
/// contract storage does not alias with other accesses.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
cell: TypedCell::new_unchecked(key),
cache: Default::default(),
}
}
/// Allocates a new sync cell using the given storage allocator.
///
/// # Safety
......@@ -258,18 +244,26 @@ where
#[cfg(all(test, feature = "test-env"))]
mod tests {
use super::*;
use crate::storage::Key;
use crate::{
test_utils::run_test,
env::TestEnv,
};
fn dummy_cell() -> SyncCell<i32> {
unsafe {
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
SyncCell::new_using_alloc(&mut alloc)
}
}
#[test]
fn simple() {
run_test(|| {
let mut cell: SyncCell<i32> = unsafe {
SyncCell::new_unchecked(Key([0x42; 32]))
};
let mut cell = dummy_cell();
assert_eq!(cell.get(), None);
cell.set(5);
assert_eq!(cell.get(), Some(&5));
......@@ -283,9 +277,7 @@ mod tests {
#[test]
fn count_reads() {
run_test(|| {
let cell: SyncCell<i32> = unsafe {
SyncCell::new_unchecked(Key([0x42; 32]))
};
let mut cell = dummy_cell();
assert_eq!(TestEnv::total_reads(), 0);
cell.get();
assert_eq!(TestEnv::total_reads(), 1);
......@@ -298,9 +290,7 @@ mod tests {
#[test]
fn count_writes() {
run_test(|| {
let mut cell: SyncCell<i32> = unsafe {
SyncCell::new_unchecked(Key([0x42; 32]))
};
let mut cell = dummy_cell();
assert_eq!(TestEnv::total_writes(), 0);
cell.set(1);
assert_eq!(TestEnv::total_writes(), 1);
......
......@@ -16,7 +16,6 @@
use crate::{
storage::{
Key,
NonCloneMarker,
cell::RawCell,
Allocator,
......@@ -58,19 +57,6 @@ impl<T> parity_codec::Decode for TypedCell<T> {
}
impl<T> TypedCell<T> {
/// Creates a new typed cell for the given key.
///
/// # Safety
///
/// This is unsafe since it does not check if the associated
/// contract storage does not alias with other accesses.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
cell: RawCell::new_unchecked(key),
non_clone: NonCloneMarker::default()
}
}
/// Allocates a new typed cell using the given storage allocator.
///
/// # Safety
......@@ -116,18 +102,26 @@ where
#[cfg(all(test, feature = "test-env"))]
mod tests {
use super::*;
use crate::storage::Key;
use crate::{
test_utils::run_test,
env::TestEnv,
};
fn dummy_cell() -> TypedCell<i32> {
unsafe {
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
TypedCell::new_using_alloc(&mut alloc)
}
}
#[test]
fn simple() {
run_test(|| {
let mut cell: TypedCell<i32> = unsafe {
TypedCell::new_unchecked(Key([0x42; 32]))
};
let mut cell = dummy_cell();
assert_eq!(cell.load(), None);
cell.store(&5);
assert_eq!(cell.load(), Some(5));
......@@ -139,9 +133,7 @@ mod tests {
#[test]
fn count_reads() {
run_test(|| {
let cell: TypedCell<i32> = unsafe {
TypedCell::new_unchecked(Key([0x42; 32]))
};
let mut cell = dummy_cell();
assert_eq!(TestEnv::total_reads(), 0);
cell.load();
assert_eq!(TestEnv::total_reads(), 1);
......@@ -154,9 +146,7 @@ mod tests {
#[test]
fn count_writes() {
run_test(|| {
let mut cell: TypedCell<i32> = unsafe {
TypedCell::new_unchecked(Key([0x42; 32]))
};
let mut cell = dummy_cell();
assert_eq!(TestEnv::total_writes(), 0);
cell.store(&1);
assert_eq!(TestEnv::total_writes(), 1);
......
......@@ -104,7 +104,7 @@ impl RawChunk {
/// - ... it does not check if the associated
/// contract storage does not alias with other accesses.
/// - ... it does not check if given capacity is non zero.
pub unsafe fn new_unchecked(key: Key) -> Self {
unsafe fn new_unchecked(key: Key) -> Self {
Self{
key,
non_clone: Default::default(),
......@@ -115,16 +115,13 @@ impl RawChunk {
///
/// # Safety
///
/// The is unsafe because it does not check if the associated storage
/// This is unsafe because it does not check if the associated storage
/// does not alias with storage allocated by other storage allocators.
pub unsafe fn new_using_alloc<A>(alloc: &mut A) -> Self
where
A: Allocator
{
Self{
key: alloc.alloc(u32::max_value()),
non_clone: Default::default(),
}
Self::new_unchecked(alloc.alloc(u32::max_value()))
}
/// Returns the unterlying key to the cells.
......
......@@ -292,21 +292,6 @@ impl<T> parity_codec::Decode for SyncChunk<T> {
}
impl<T> SyncChunk<T> {
/// Creates a new mutable cell chunk for the given key and capacity.
///
/// # Safety
///
/// This is unsafe because ..
/// - .. it does not check if the associated
/// contract storage does not alias with other accesses.
/// - .. it does not check if given capacity is non zero.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
chunk: TypedChunk::new_unchecked(key),
elems: Cache::default(),
}
}
/// Allocates a new sync cell chunk using the given storage allocator.
///
/// # Safety
......@@ -460,14 +445,21 @@ mod tests {
env::TestEnv,
};
fn dummy_chunk() -> SyncChunk<u32> {
unsafe {
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
SyncChunk::new_using_alloc(&mut alloc)
}
}
#[test]
fn simple() {
run_test(|| {
const TEST_LEN: u32 = 5;
let mut chunk = unsafe {
SyncChunk::new_unchecked(Key([0x42; 32]))
};
let mut chunk = dummy_chunk();
// Invariants after initialization
for i in 0..TEST_LEN {
......@@ -495,9 +487,7 @@ mod tests {
run_test(|| {
const TEST_LEN: u32 = 5;
let mut chunk = unsafe {
SyncChunk::new_unchecked(Key([0x42; 32]))
};
let mut chunk = dummy_chunk();
// Reads and writes after init.
assert_eq!(TestEnv::total_reads(), 0);
......@@ -544,9 +534,7 @@ mod tests {
#[test]
fn replace() {
run_test(|| {
let mut chunk = unsafe {
SyncChunk::new_unchecked(Key([0x42; 32]))
};
let mut chunk = dummy_chunk();
// Replace some with none.
assert_eq!(chunk.replace(0, 42), None);
......@@ -562,9 +550,7 @@ mod tests {
#[test]
fn remove() {
run_test(|| {
let mut chunk = unsafe {
SyncChunk::new_unchecked(Key([0x42; 32]))
};
let mut chunk = dummy_chunk();
// Remove at none.
assert_eq!(chunk.remove(0), None);
......
......@@ -123,21 +123,6 @@ impl<T> parity_codec::Decode for TypedChunk<T> {
}
impl<T> TypedChunk<T> {
/// Creates a new typed cell chunk for the given key and length.
///
/// # Safety
///
/// This is unsafe because ..
/// - .. it does not check if the associated
/// contract storage does not alias with other accesses.
/// - .. it does not check if given length is non zero.
pub unsafe fn new_unchecked(key: Key) -> Self {
Self{
chunk: RawChunk::new_unchecked(key),
non_clone: Default::default(),
}
}
/// Allocates a new typed cell chunk using the given storage allocator.
///
/// # Safety
......@@ -226,7 +211,10 @@ mod tests {
const TEST_LEN: u32 = 5;
let mut chunk = unsafe {
TypedChunk::new_unchecked(Key([0x42; 32]))
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
TypedChunk::new_using_alloc(&mut alloc)
};
// Invariants after initialization
......@@ -254,7 +242,10 @@ mod tests {
const TEST_LEN: u32 = 5;
let mut chunk = unsafe {
TypedChunk::new_unchecked(Key([0x42; 32]))
let mut alloc = crate::storage::alloc::ForwardAlloc::from_raw_parts(
Key([0x0; 32])
);
TypedChunk::new_using_alloc(&mut alloc)
};
// Reads and writes after init.
......
......@@ -17,7 +17,6 @@
use crate::{
storage::{
self,
Key,
cell::SyncCell,
},
};
......@@ -48,25 +47,6 @@ pub struct Value<T> {
cell: SyncCell<T>,
}
impl<T> Value<T> {
/// Creates a new storage value for the given key.
///
/// # Safety
///
/// This is an inherently unsafe operation since it does not check
/// for the storage values's invariances, such as
///
/// - Is the storage region determined by the given key aliasing?
/// - Is the storage region correctly formatted to be used as storage value?
///
/// Users should not use this routine directly if possible.
pub unsafe fn from_raw_parts(key: Key) -> Self {
Self{
cell: SyncCell::new_unchecked(key)
}
}
}
impl<T> Value<T>
where
T: parity_codec::Codec + Default
......@@ -341,6 +321,7 @@ where
#[cfg(all(test, feature = "test-env"))]
mod tests {
use super::*;
use crate::storage::Key;
use crate::{
test_utils::run_test,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment