Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
VeaaC committed Mar 4, 2020
1 parent c98ac50 commit 25c8b82
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 89 deletions.
43 changes: 12 additions & 31 deletions flatdata-rs/lib/src/arrayview.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::structs::Struct;

use std::fmt;

/// Enhanced slices of flatdata Structs so that they can be created from bytes / converted to bytes
pub trait SliceExt<'a>
where
Expand Down Expand Up @@ -46,30 +44,6 @@ where
}
}

pub(crate) fn debug_format<'a, T>(
name: &str,
iter: impl Iterator<Item = &'a T> + ExactSizeIterator,
f: &mut fmt::Formatter,
) -> fmt::Result
where
T: Struct + std::fmt::Debug,
{
let len = iter.len();
let preview: Vec<_> = iter.take(super::DEBUG_PREVIEW_LEN).collect();
write!(
f,
"{} {{ len: {}, data: {:?}{} }}",
name,
len,
preview,
if len <= super::DEBUG_PREVIEW_LEN {
""
} else {
"..."
}
)
}

#[cfg(test)]
#[allow(dead_code)]
mod test {
Expand All @@ -84,17 +58,24 @@ mod test {
#[test]
fn range() {
let mut vec: Vector<R> = Vector::with_len(3);
vec.at_mut(0).set_first_x(10);
vec.at_mut(1).set_first_x(20);
vec.at_mut(2).set_first_x(30);
vec[0].set_first_x(10);
vec[1].set_first_x(20);
vec[2].set_first_x(30);

let view = vec.as_view();
assert_eq!(view.len(), 2);
assert_eq!(view.len(), 3);
assert_eq!(view[0].x(), 10..20);
assert_eq!(view[1].x(), 20..30);
assert_eq!(view[2].x(), 30..0);

assert_eq!(view[0..1].len(), 1);
assert_eq!(view[0..1][0].x(), 10..20);

use SliceExt;
let view = <&[R]>::from_bytes(view.as_bytes()).unwrap();
assert_eq!(view.len(), 2);
assert_eq!(view[0].x(), 10..20);
assert_eq!(view[1].x(), 20..30);
}

#[test]
Expand Down Expand Up @@ -137,7 +118,7 @@ mod test {
fn create_values(size: usize) -> Vector<B> {
let mut v: Vector<B> = Vector::with_len(size);
for i in 0..size as u32 {
let a = v.at_mut(i as usize);
let a = &mut v[i as usize];
a.set_id(i);
}
v
Expand Down
109 changes: 51 additions & 58 deletions flatdata-rs/lib/src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{arrayview::debug_format, error::ResourceStorageError, structs::Struct, SliceExt};
use crate::{error::ResourceStorageError, structs::Struct, SliceExt};

use crate::storage::ResourceHandle;

Expand Down Expand Up @@ -90,24 +90,6 @@ where
Self { data }
}

/// Size of the vector in bytes.
#[inline]
pub fn size_in_bytes(&self) -> usize {
self.len() * <T as Struct>::SIZE_IN_BYTES
}

/// Number of elements in the vector.
#[inline]
pub fn len(&self) -> usize {
self.data.len() - 1
}

/// Returns `true` if the vector has a length 0.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Reserves capacity for at least `additional` more elements to be
/// inserted in the given vector. The collection may reserve more space
/// to avoid frequent reallocations. After calling reserve, capacity
Expand All @@ -118,20 +100,18 @@ where
self.data.reserve(self.data.len() + additional)
}

/// Returns an `ArrayView` to this vector.
/// Returns a slice for the vector contents
#[inline]
pub fn as_view(&self) -> &[T] {
if T::IS_OVERLAPPING_WITH_NEXT {
&self.data[..self.data.len().saturating_sub(2)]
} else {
&self.data[..self.data.len() - 1]
}
let len = self.data.len() - 1;
&self.data[0..len]
}

/// Returns the contents of this vector as slice of bytes.
/// Returns a slice for the vector contents
#[inline]
pub fn as_bytes(&self) -> &[u8] {
self.as_view().as_bytes()
pub fn as_view_mut(&mut self) -> &mut [T] {
let len = self.data.len() - 1;
&mut self.data[0..len]
}

/// Appends an element to the end of this vector and returns a mutable
Expand All @@ -142,21 +122,34 @@ where
self.data.push(unsafe { T::create_unchecked() });
&mut self.data[next]
}
}

/// Return an accessor handle to the element at position `index` in the
/// vector.
#[inline]
pub fn at(&self, index: usize) -> &T {
assert!(index < self.len());
&self.data[index]
impl<T> AsRef<[T]> for Vector<T>
where
T: Struct,
{
fn as_ref(&self) -> &[T] {
self.as_view()
}
}

/// Return a mutable handle to the element at position `index` in the
/// vector.
#[inline]
pub fn at_mut(&mut self, index: usize) -> &mut T {
assert!(index < self.len());
&mut self.data[index]
impl<T> std::ops::Deref for Vector<T>
where
T: Struct,
{
type Target = [T];

fn deref(&self) -> &[T] {
self.as_view()
}
}

impl<T> std::ops::DerefMut for Vector<T>
where
T: Struct,
{
fn deref_mut(&mut self) -> &mut [T] {
self.as_view_mut()
}
}

Expand All @@ -179,7 +172,7 @@ where
///
/// [`as_bytes`]: #method.as_bytes
fn as_ref(&self) -> &[u8] {
self.as_bytes()
self.as_view().as_bytes()
}
}

Expand All @@ -188,7 +181,7 @@ where
T: Struct + std::fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
debug_format("Vector", self.as_view().iter(), f)
self.as_view().fmt(f)
}
}

Expand Down Expand Up @@ -286,7 +279,7 @@ where
///
/// [`flush`]: #method.flush
pub fn grow(&mut self) -> io::Result<&mut T> {
if self.data.as_bytes().len() > 1024 * 1024 * 32 {
if self.data.as_view().as_bytes().len() > 1024 * 1024 * 32 {
self.flush()?;
}
self.len += 1;
Expand All @@ -297,7 +290,7 @@ where
fn flush(&mut self) -> io::Result<()> {
self.resource_handle
.borrow_mut()
.write(&self.data.as_bytes())?;
.write(&self.data.as_view().as_bytes())?;
self.data.clear();
Ok(())
}
Expand Down Expand Up @@ -344,37 +337,37 @@ mod tests {
#[test]
fn test_vector_range() {
let mut v: Vector<R> = Vector::with_len(3);
v.at_mut(0).set_first_x(10);
v.at_mut(1).set_first_x(20);
v.at_mut(2).set_first_x(30);
v[0].set_first_x(10);
v[1].set_first_x(20);
v[2].set_first_x(30);

assert_eq!(v.at(0).x(), 10..20);
assert_eq!(v.at(1).x(), 20..30);
assert_eq!(v.at(2).x(), 30..0);
assert_eq!(v[0].x(), 10..20);
assert_eq!(v[1].x(), 20..30);
assert_eq!(v[2].x(), 30..0);
}

#[test]
fn test_vector_index() {
let mut v: Vector<A> = Vector::with_len(2);
assert_eq!(v.len(), 2);
{
let a = v.at_mut(0);
let a = &mut v[0];
a.set_x(1);
a.set_y(2);
assert_eq!(a.x(), 1);
assert_eq!(a.y(), 2);
}
{
let b = v.at_mut(1);
let b = &mut v[1];
b.set_x(3);
b.set_y(4);
assert_eq!(b.x(), 3);
assert_eq!(b.y(), 4);
}
let a = v.at(0);
let a = &mut v[0];
assert_eq!(a.x(), 1);
assert_eq!(a.y(), 2);
let b = v.at(1);
let b = &mut v[1];
assert_eq!(b.x(), 3);
assert_eq!(b.y(), 4);
}
Expand All @@ -384,7 +377,7 @@ mod tests {
let mut v: Vector<A> = Vector::with_len(1);
assert_eq!(v.len(), 1);
{
let a = v.at_mut(0);
let a = &mut v[0];
a.set_x(1);
assert_eq!(a.x(), 1);
a.set_y(2);
Expand All @@ -401,7 +394,7 @@ mod tests {
let mut v: Vector<A> = Vector::with_len(1);
assert_eq!(v.len(), 1);
{
let a = v.at_mut(0);
let a = &mut v[0];
a.set_x(1);
a.set_y(2);
assert_eq!(a.x(), 1);
Expand All @@ -416,10 +409,10 @@ mod tests {
}
{
assert_eq!(v.len(), 2);
let a = &v.at(0);
let a = &v[0];
assert_eq!(a.x(), 1);
assert_eq!(a.y(), 2);
let b = &v.at(1);
let b = &v[1];
assert_eq!(b.x(), 3);
assert_eq!(b.y(), 4);
}
Expand Down

0 comments on commit 25c8b82

Please sign in to comment.