Skip to content

Commit

Permalink
Add 'raw' module with get_struct_data_section(), get_list_bytes(), an…
Browse files Browse the repository at this point in the history
…d other functions.
  • Loading branch information
dwrensha committed Dec 9, 2018
1 parent 938d60e commit 6683d7a
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 4 deletions.
6 changes: 6 additions & 0 deletions capnp/src/data_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ impl <'a> Reader<'a> {
}
}

impl <'a> ::traits::IntoInternalListReader<'a> for Reader<'a> {
fn into_internal_list_reader(self) -> ListReader<'a> {
self.reader
}
}

pub struct Builder<'a> {
builder: ListBuilder<'a>
}
Expand Down
6 changes: 6 additions & 0 deletions capnp/src/enum_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl <'a, T : FromU16> Reader<'a, T> {
}
}

impl <'a, T> ::traits::IntoInternalListReader<'a> for Reader<'a, T> where T: PrimitiveElement {
fn into_internal_list_reader(self) -> ListReader<'a> {
self.reader
}
}

pub struct Builder<'a, T> {
marker: PhantomData<T>,
builder: ListBuilder<'a>
Expand Down
1 change: 1 addition & 0 deletions capnp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub mod list_list;
pub mod message;
pub mod primitive_list;
pub mod private;
pub mod raw;
pub mod serialize;
pub mod serialize_packed;
pub mod struct_list;
Expand Down
6 changes: 6 additions & 0 deletions capnp/src/primitive_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ impl <'a, T: PrimitiveElement> Reader<'a, T> {
}
}

impl <'a, T> ::traits::IntoInternalListReader<'a> for Reader<'a, T> where T: PrimitiveElement {
fn into_internal_list_reader(self) -> ListReader<'a> {
self.reader
}
}

pub struct Builder<'a, T> where T: PrimitiveElement {
marker: marker::PhantomData<T>,
builder: ListBuilder<'a>
Expand Down
24 changes: 23 additions & 1 deletion capnp/src/private/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use {MessageSize, Result, Word};
pub use self::ElementSize::{Void, Bit, Byte, TwoBytes, FourBytes, EightBytes, Pointer, InlineComposite};

#[repr(u8)]
#[derive(Clone, Copy, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ElementSize {
Void = 0,
Bit = 1,
Expand Down Expand Up @@ -2999,6 +2999,28 @@ impl <'a> ListReader<'a> {
#[inline]
pub fn len(&self) -> ElementCount32 { self.element_count }

pub(crate) fn get_step_size_in_bits(&self) -> u32 {
self.step
}

pub(crate) fn get_element_size(&self) -> ElementSize {
self.element_size
}

pub(crate) fn into_raw_bytes(self) -> &'a [u8] {
if self.element_count == 0 {
// Explictly handle this case to avoid forming a slice to a null pointer,
// which would be undefined behavior.
&[]
} else {
let num_bytes = wire_helpers::round_bits_up_to_bytes(
self.step as u64 * self.element_count as u64) as usize;
unsafe {
::std::slice::from_raw_parts(self.ptr, num_bytes)
}
}
}

#[inline]
pub fn get_struct_element(&self, index: ElementCount32) -> StructReader<'a> {
let index_byte: ByteCount32 =
Expand Down
53 changes: 53 additions & 0 deletions capnp/src/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2018 the capnproto-rust contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//! Functions providing low level access to encoded data.

use traits::{IntoInternalListReader, IntoInternalStructReader};

/// Gets a slice view of the data section of a struct.
pub fn get_struct_data_section<'a, T>(value: T) -> &'a [u8]
where T: IntoInternalStructReader<'a>
{
value.into_internal_struct_reader().get_data_section_as_blob()
}

// TODO: get_struct_pointer_section()

/// Gets the size of the elements in a list.
pub fn get_list_element_size<'a, T>(value: T) -> ::private::layout::ElementSize
where T: IntoInternalListReader<'a>
{
value.into_internal_list_reader().get_element_size()
}

/// Gets the number of bits between successive elements in a list.
pub fn get_list_step_size_in_bits<'a, T>(value: T) -> u32
where T: IntoInternalListReader<'a>
{
value.into_internal_list_reader().get_step_size_in_bits()
}

/// Gets a slice view of a list, exluding any tag word.
pub fn get_list_bytes<'a, T>(value: T) -> &'a [u8]
where T: IntoInternalListReader<'a>
{
value.into_internal_list_reader().into_raw_bytes()
}
6 changes: 6 additions & 0 deletions capnp/src/struct_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ impl <'a, T> Reader<'a, T> where T: for<'b> ::traits::OwnedStruct<'b> {
}
}

impl <'a, T> ::traits::IntoInternalListReader<'a> for Reader<'a, T> where T: for<'b> ::traits::OwnedStruct<'b> {
fn into_internal_list_reader(self) -> ListReader<'a> {
self.reader
}
}

pub struct Builder<'a, T> where T: for<'b> ::traits::OwnedStruct<'b> {
marker: PhantomData<T>,
builder: ListBuilder<'a>
Expand Down
6 changes: 6 additions & 0 deletions capnp/src/text_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ impl <'a> Reader<'a> {
}
}

impl <'a> ::traits::IntoInternalListReader<'a> for Reader<'a> {
fn into_internal_list_reader(self) -> ListReader<'a> {
self.reader
}
}

pub struct Builder<'a> {
builder: ListBuilder<'a>
}
Expand Down
7 changes: 6 additions & 1 deletion capnp/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
// THE SOFTWARE.

use {Word, Result};
use private::layout::{CapTable, StructReader, StructBuilder, StructSize, PointerBuilder, PointerReader};
use private::layout::{CapTable, ListReader, StructReader, StructBuilder, StructSize,
PointerBuilder, PointerReader};

use std::marker::PhantomData;

Expand All @@ -41,6 +42,10 @@ pub trait FromStructBuilder<'a> {
fn new(struct_builder: StructBuilder<'a>) -> Self;
}

pub trait IntoInternalListReader<'a> {
fn into_internal_list_reader(self) -> ListReader<'a>;
}

pub trait FromPointerReader<'a> : Sized {
fn get_from_pointer(reader: &PointerReader<'a>) -> Result<Self>;
}
Expand Down
48 changes: 46 additions & 2 deletions capnpc/test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ mod tests {

#[test]
fn test_prim_list () {

use test_capnp::test_prim_list;

// Make the first segment small to force allocation of a second segment.
Expand Down Expand Up @@ -127,7 +126,6 @@ mod tests {

#[test]
fn test_struct_list () {

use test_capnp::test_struct_list;

let mut message = message::Builder::new(message::HeapAllocator::new());
Expand Down Expand Up @@ -1345,4 +1343,50 @@ mod tests {
assert_eq!(11, list_reader.get(0).get_int8_field());
::test_util::CheckTestMessage::check_test_message(list_reader.get(1));
}

#[test]
fn get_raw_struct_data() {
use capnp::traits::HasStructSize;
use test_capnp::test_all_types;
let mut message = message::Builder::new_default();
let mut root: test_all_types::Builder = message.init_root();
root.set_int8_field(3);
root.set_int16_field(0x0abb);
let struct_size = <test_all_types::Builder as HasStructSize>::struct_size();
{
let raw_bytes =
::capnp::raw::get_struct_data_section(root.reborrow().into_reader());
assert_eq!(raw_bytes.len(), (struct_size.data * 8) as usize);
assert_eq!(raw_bytes[0], 0); // boolField
assert_eq!(raw_bytes[1], 3); // int8Field
assert_eq!(raw_bytes[2], 0xbb); // int16Field less significant byte
assert_eq!(raw_bytes[3], 0x0a); // int16Field more significant byte
}
}

#[test]
fn get_raw_list_data() {
use test_capnp::test_all_types;
let mut message = message::Builder::new_default();
let mut root: test_all_types::Builder = message.init_root();
{
let mut uint16_list = root.reborrow().init_u_int16_list(5);
uint16_list.set(0, 10);
uint16_list.set(1, 11);
uint16_list.set(2, 12);
uint16_list.set(3, 13);
uint16_list.set(4, 14);
assert_eq!(
::capnp::raw::get_list_element_size(uint16_list.reborrow().into_reader()),
::capnp::private::layout::ElementSize::TwoBytes);

assert_eq!(
::capnp::raw::get_list_step_size_in_bits(uint16_list.reborrow().into_reader()),
16);

assert_eq!(
::capnp::raw::get_list_bytes(uint16_list.reborrow().into_reader()),
&[10, 0, 11, 0, 12, 0, 13, 0, 14, 0]);
}
}
}

0 comments on commit 6683d7a

Please sign in to comment.