Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress (do not land): expose arena as type parameter #210

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
dwrensha committed Dec 5, 2020
commit 90fc7f4d5b392864dfee1488fdd8d928fb5717a1
55 changes: 28 additions & 27 deletions capnp/src/any_pointer.rs
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ use alloc::boxed::Box;
use alloc::vec::Vec;

use crate::capability::FromClientHook;
use crate::private::arena::{BuilderArena, ReaderArena};
use crate::private::capability::{ClientHook, PipelineHook, PipelineOp};
use crate::private::layout::{PointerReader, PointerBuilder};
use crate::traits::{FromPointerReader, FromPointerBuilder, SetPointerBuilder};
@@ -33,23 +34,23 @@ use crate::Result;
#[derive(Copy, Clone)]
pub struct Owned(());

impl <'a> crate::traits::Owned<'a> for Owned {
type Reader = Reader<'a>;
type Builder = Builder<'a>;
impl <'a, A: 'a> crate::traits::Owned<'a, A> for Owned {
type Reader = Reader<'a, A>;
type Builder = Builder<'a, A>;
}

impl crate::traits::Pipelined for Owned {
type Pipeline = Pipeline;
}

#[derive(Copy, Clone)]
pub struct Reader<'a> {
reader: PointerReader<'a>
pub struct Reader<'a, A> {
reader: PointerReader<&'a A>
}

impl <'a> Reader<'a> {
impl <'a, A> Reader<'a, A> where A: ReaderArena {
#[inline]
pub fn new<'b>(reader: PointerReader<'b>) -> Reader<'b> {
pub fn new<'b>(reader: PointerReader<&'b A>) -> Reader<'b, A> {
Reader { reader: reader }
}

@@ -64,7 +65,7 @@ impl <'a> Reader<'a> {
}

#[inline]
pub fn get_as<T: FromPointerReader<'a>>(&self) -> Result<T> {
pub fn get_as<T: FromPointerReader<'a, A>>(&self) -> Result<T> {
FromPointerReader::get_from_pointer(&self.reader, None)
}

@@ -90,19 +91,19 @@ impl <'a> Reader<'a> {
}
}

impl <'a> FromPointerReader<'a> for Reader<'a> {
fn get_from_pointer(reader: &PointerReader<'a>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a>> {
impl <'a, A> FromPointerReader<'a, A> for Reader<'a, A> where A: ReaderArena {
fn get_from_pointer(reader: &PointerReader<&'a A>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a, A>> {
if default.is_some() {
panic!("Unsupported: any_pointer with a default value.");
}
Ok(Reader { reader: *reader })
}
}

impl <'a> crate::traits::SetPointerBuilder<Builder<'a>> for Reader<'a> {
fn set_pointer_builder<'b>(mut pointer: crate::private::layout::PointerBuilder<'b>,
value: Reader<'a>,
canonicalize: bool) -> Result<()> {
impl <'a, A> crate::traits::SetPointerBuilder<Builder<'a, A>> for Reader<'a, A> {
fn set_pointer_builder<'b, B>(mut pointer: crate::private::layout::PointerBuilder<&'b mut B>,
value: Reader<'a, A>,
canonicalize: bool) -> Result<()> {
pointer.copy_from(value.reader, canonicalize)
}
}
@@ -113,17 +114,17 @@ impl <'a> crate::traits::Imbue<'a> for Reader<'a> {
}
}

pub struct Builder<'a> {
builder: PointerBuilder<'a>
pub struct Builder<'a, A> {
builder: PointerBuilder<&mut 'a A>
}

impl <'a> Builder<'a> {
impl <'a, A> Builder<'a, A> where A: BuilderArena {
#[inline]
pub fn new(builder: PointerBuilder<'a>) -> Builder<'a> {
pub fn new(builder: PointerBuilder<&'a mut A>) -> Builder<'a, A> {
Builder { builder: builder }
}

pub fn reborrow<'b>(&'b mut self) -> Builder<'b> {
pub fn reborrow<'b>(&'b mut self) -> Builder<'b, A> {
Builder { builder: self.builder.borrow() }
}

@@ -136,15 +137,15 @@ impl <'a> Builder<'a> {
self.builder.into_reader().total_size()
}

pub fn get_as<T : FromPointerBuilder<'a>>(self) -> Result<T> {
pub fn get_as<T: FromPointerBuilder<'a, A>>(self) -> Result<T> {
FromPointerBuilder::get_from_pointer(self.builder, None)
}

pub fn init_as<T : FromPointerBuilder<'a>>(self) -> T {
pub fn init_as<T: FromPointerBuilder<'a, A>>(self) -> T {
FromPointerBuilder::init_pointer(self.builder, 0)
}

pub fn initn_as<T: FromPointerBuilder<'a>>(self, size: u32) -> T {
pub fn initn_as<T: FromPointerBuilder<'a, A>>(self, size: u32) -> T {
FromPointerBuilder::init_pointer(self.builder, size)
}

@@ -162,27 +163,27 @@ impl <'a> Builder<'a> {
self.builder.clear()
}

pub fn into_reader(self) -> Reader<'a> {
pub fn into_reader(self) -> Reader<'a, A> {
Reader { reader: self.builder.into_reader() }
}
}

impl <'a> FromPointerBuilder<'a> for Builder<'a> {
fn init_pointer(mut builder: PointerBuilder<'a>, _len: u32) -> Builder<'a> {
impl <'a, A> FromPointerBuilder<'a, A> for Builder<'a, A> {
fn init_pointer(mut builder: PointerBuilder<&mut 'a A>, _len: u32) -> Builder<'a, A> {
if !builder.is_null() {
builder.clear();
}
Builder { builder: builder }
}
fn get_from_pointer(builder: PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a>> {
fn get_from_pointer(builder: PointerBuilder<&mut 'a A>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a, A>> {
if default.is_some() {
panic!("AnyPointer defaults are unsupported")
}
Ok(Builder { builder: builder })
}
}

impl <'a> crate::traits::ImbueMut<'a> for Builder<'a> {
impl <'a, A> crate::traits::ImbueMut<'a> for Builder<'a, A> {
fn imbue_mut(&mut self, cap_table: &'a mut crate::private::layout::CapTable) {
self.builder.imbue(crate::private::layout::CapTableBuilder::Plain(cap_table));
}
21 changes: 13 additions & 8 deletions capnp/src/capability.rs
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ use core::ops::Try;

use crate::{any_pointer, Error, MessageSize};
use crate::traits::{Pipelined, Owned};
use crate::private::arena::ReaderArena;
use crate::private::capability::{ClientHook, ParamsHook, RequestHook, ResponseHook, ResultsHook};

/// A computation that might eventually resolve to a value of type `T` or to an error
@@ -101,20 +102,21 @@ impl<T> Try for Promise<T, crate::Error> {
Promise::ok(v)
}
}

/*
/// A promise for a result from a method call.
#[must_use]
pub struct RemotePromise<Results> where Results: Pipelined + for<'a> Owned<'a> + 'static {
pub struct RemotePromise<Results> where Results: Pipelined + for<'a> Owned<'a, dyn ReaderArena> + 'static {
pub promise: Promise<Response<Results>, crate::Error>,
pub pipeline: Results::Pipeline,
}

*/
/// A response from a method call, as seen by the client.
pub struct Response<Results> {
pub marker: PhantomData<Results>,
pub hook: Box<dyn ResponseHook>,
}

/*
impl <Results> Response<Results>
where Results: Pipelined + for<'a> Owned<'a>
{
@@ -125,13 +127,15 @@ impl <Results> Response<Results>
self.hook.get()?.get_as()
}
}
*/

/// A method call that has not been sent yet.
pub struct Request<Params, Results> {
pub marker: PhantomData<(Params, Results)>,
pub hook: Box<dyn RequestHook>
}

/*
impl <Params, Results> Request<Params, Results>
where Params: for<'a> Owned<'a>
{
@@ -164,6 +168,7 @@ where Results: Pipelined + for<'a> Owned<'a> + 'static + Unpin,
}
}
}
*/

/// The values of the parameters passed to a method call, as seen by the server.
pub struct Params<T> {
@@ -175,11 +180,11 @@ impl <T> Params <T> {
pub fn new(hook: Box<dyn ParamsHook>) -> Params<T> {
Params { marker: PhantomData, hook: hook }
}
pub fn get<'a>(&'a self) -> crate::Result<<T as Owned<'a>>::Reader>
/* pub fn get<'a>(&'a self) -> crate::Result<<T as Owned<'a>>::Reader>
where T: Owned<'a>
{
Ok(self.hook.get()?.get_as()?)
}
}*/
}

/// The return values of a method, written in-place by the method body.
@@ -189,20 +194,20 @@ pub struct Results<T> {
}

impl <T> Results<T>
where T: for<'a> Owned<'a>
// where T: for<'a> Owned<'a>
{
pub fn new(hook: Box<dyn ResultsHook>) -> Results<T> {
Results { marker: PhantomData, hook: hook }
}

/*
pub fn get<'a>(&'a mut self) -> <T as Owned<'a>>::Builder {
self.hook.get().unwrap().get_as().unwrap()
}
pub fn set(&mut self, other: <T as Owned>::Reader) -> crate::Result<()>
{
self.hook.get().unwrap().set_as(other)
}
}*/
}

pub trait FromTypelessPipeline {
18 changes: 9 additions & 9 deletions capnp/src/data.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ use crate::Result;
#[derive(Copy, Clone)]
pub struct Owned(());

impl<'a> crate::traits::Owned<'a> for Owned {
impl<'a, A> crate::traits::Owned<'a, A> for Owned {
type Reader = Reader<'a>;
type Builder = Builder<'a>;
}
@@ -43,8 +43,8 @@ pub(crate) unsafe fn reader_from_raw_parts<'a>(p: *const u8, len: u32) -> Reader
::core::slice::from_raw_parts(p, len as usize)
}

impl <'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
fn get_from_pointer(reader: &PointerReader<'a>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a>> {
impl <'a, A> crate::traits::FromPointerReader<'a, A> for Reader<'a> {
fn get_from_pointer(reader: PointerReader<&'a A>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a>> {
reader.get_data(default)
}
}
@@ -60,19 +60,19 @@ pub(crate) unsafe fn builder_from_raw_parts<'a>(p: *mut u8, len: u32) -> Builder
::core::slice::from_raw_parts_mut(p, len as usize)
}

impl <'a> crate::traits::FromPointerBuilder<'a> for Builder<'a> {
fn init_pointer(builder : PointerBuilder<'a>, size : u32) -> Builder<'a> {
impl <'a, A> crate::traits::FromPointerBuilder<'a, A> for Builder<'a> {
fn init_pointer(builder: PointerBuilder<&'a mut A>, size : u32) -> Builder<'a> {
builder.init_data(size)
}
fn get_from_pointer(builder :PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a>> {
fn get_from_pointer(builder: PointerBuilder<&'a mut A>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a>> {
builder.get_data(default)
}
}

impl <'a> crate::traits::SetPointerBuilder<Builder<'a>> for Reader<'a> {
fn set_pointer_builder<'b>(pointer: PointerBuilder<'b>,
value: Reader<'a>,
_canonicalize: bool) -> Result<()> {
fn set_pointer_builder<'b, B>(pointer: PointerBuilder<&'b mut B>,
value: Reader<'a>,
_canonicalize: bool) -> Result<()> {
pointer.set_data(value);
Ok(())
}
18 changes: 9 additions & 9 deletions capnp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -33,24 +33,24 @@
extern crate alloc;

pub mod any_pointer;
pub mod any_pointer_list;
//pub mod any_pointer_list;
pub mod capability;
pub mod capability_list;
pub mod constant;
//pub mod capability_list;
//pub mod constant;
pub mod data;
pub mod data_list;
pub mod enum_list;
//pub mod data_list;
//pub mod enum_list;
pub mod io;
pub mod list_list;
//pub mod list_list;
pub mod message;
pub mod primitive_list;
pub mod private;
pub mod raw;
//pub mod raw;
pub mod serialize;
pub mod serialize_packed;
pub mod struct_list;
//pub mod struct_list;
pub mod text;
pub mod text_list;
//pub mod text_list;
pub mod traits;

use alloc::string::String;
28 changes: 14 additions & 14 deletions capnp/src/message.rs
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ use alloc::vec::Vec;
use core::convert::From;

use crate::any_pointer;
use crate::private::arena::{BuilderArenaImpl, ReaderArenaImpl, BuilderArena, ReaderArena};
use crate::private::arena::{BuilderArenaImpl, ReaderArenaImpl, ReadLimiterImpl, BuilderArena, ReaderArena};
use crate::private::layout;
use crate::private::units::BYTES_PER_WORD;
use crate::traits::{FromPointerReader, FromPointerBuilder, SetPointerBuilder, Owned};
@@ -152,7 +152,7 @@ impl <'b> ReaderSegments for [&'b [u8]] {

/// A container used to read a message.
pub struct Reader<S> where S: ReaderSegments {
arena: ReaderArenaImpl<S, crate::private::arena::ReadLimiterImpl>,
arena: ReaderArenaImpl<S, ReadLimiterImpl>,
}

impl <S> Reader<S> where S: ReaderSegments {
@@ -162,15 +162,15 @@ impl <S> Reader<S> where S: ReaderSegments {
}
}

fn get_root_internal<'a>(&'a self) -> Result<any_pointer::Reader<'a>> {
fn get_root_internal<'a>(&'a self) -> Result<any_pointer::Reader<'a, ReaderArenaImpl<S, ReadLimiterImpl>>> {
let (segment_start, _seg_len) = self.arena.get_segment(0)?;
let pointer_reader = layout::PointerReader::get_root(
&self.arena, 0, segment_start, self.arena.nesting_limit())?;
Ok(any_pointer::Reader::new(pointer_reader))
}

/// Gets the root of the message, interpreting it as the given type.
pub fn get_root<'a, T: FromPointerReader<'a>>(&'a self) -> Result<T> {
pub fn get_root<'a, T: FromPointerReader<'a, ReaderArenaImpl<S, ReadLimiterImpl>>>(&'a self) -> Result<T> {
self.get_root_internal()?.get_as()
}

@@ -216,22 +216,22 @@ impl <S> Reader<S> where S: ReaderSegments {
Ok(result)
}

pub fn into_typed<T: for<'a> Owned<'a>>(self) -> TypedReader<S, T> {
pub fn into_typed<T: for<'a> Owned<'a, ReaderArenaImpl<S, ReadLimiterImpl>>>(self) -> TypedReader<S, T> {
TypedReader::new(self)
}
}

/// A message reader whose value is known to be of type `T`.
pub struct TypedReader<S, T>
where S: ReaderSegments,
T: for<'a> Owned<'a> {
T: for<'a> Owned<'a, ReaderArenaImpl<S, ReadLimiterImpl>> {
marker: ::core::marker::PhantomData<T>,
message: Reader<S>,
}

impl <S, T> TypedReader<S, T>
where S: ReaderSegments,
T : for<'a> Owned<'a> {
T : for<'a> Owned<'a, ReaderArenaImpl<S, ReadLimiterImpl>> {

pub fn new(message: Reader<S>) -> Self {
TypedReader {
@@ -240,7 +240,7 @@ impl <S, T> TypedReader<S, T>
}
}

pub fn get<'a> (&'a self) -> Result<<T as Owned<'a>>::Reader> {
pub fn get<'a> (&'a self) -> Result<<T as Owned<'a, ReaderArenaImpl<S, ReadLimiterImpl>>>::Reader> {
self.message.get_root()
}

@@ -251,7 +251,7 @@ impl <S, T> TypedReader<S, T>

impl <S, T> From<Reader<S>> for TypedReader<S, T>
where S: ReaderSegments,
T: for<'a> Owned<'a> {
T: for<'a> Owned<'a, ReaderArenaImpl<S, ReadLimiterImpl>> {

fn from(message: Reader<S>) -> TypedReader<S, T> {
TypedReader::new(message)
@@ -260,7 +260,7 @@ impl <S, T> From<Reader<S>> for TypedReader<S, T>

impl <A, T> From<Builder<A>> for TypedReader<Builder<A>, T>
where A: Allocator,
T: for<'a> Owned<'a> {
T: for<'a> Owned<'a, ReaderArenaImpl<message::Builder<A>, ReadLimiterImpl>> {

fn from(message: Builder<A>) -> TypedReader<Builder<A>, T> {
let reader = message.into_reader();
@@ -315,7 +315,7 @@ impl <A> Builder<A> where A: Allocator {
}
}

fn get_root_internal<'a>(&'a mut self) -> any_pointer::Builder<'a> {
fn get_root_internal<'a>(&'a mut self) -> any_pointer::Builder<'a, BuilderArenaImpl<A>> {
if self.arena.len() == 0 {
self.arena.allocate_segment(1).expect("allocate root pointer");
self.arena.allocate(0, 1).expect("allocate root pointer");
@@ -329,18 +329,18 @@ impl <A> Builder<A> where A: Allocator {
}

/// Initializes the root as a value of the given type.
pub fn init_root<'a, T: FromPointerBuilder<'a>>(&'a mut self) -> T {
pub fn init_root<'a, T: FromPointerBuilder<'a, BuilderArenaImpl<A>>>(&'a mut self) -> T {
let root = self.get_root_internal();
root.init_as()
}

/// Gets the root, interpreting it as the given type.
pub fn get_root<'a, T: FromPointerBuilder<'a>>(&'a mut self) -> Result<T> {
pub fn get_root<'a, T: FromPointerBuilder<'a, BuilderArenaImpl<A>>>(&'a mut self) -> Result<T> {
let root = self.get_root_internal();
root.get_as()
}

pub fn get_root_as_reader<'a, T: FromPointerReader<'a>>(&'a self) -> Result<T> {
pub fn get_root_as_reader<'a, T: FromPointerReader<'a, BuilderArenaImpl<A>>>(&'a self) -> Result<T> {
if self.arena.len() == 0 {
any_pointer::Reader::new(layout::PointerReader::new_default()).get_as()
} else {
63 changes: 32 additions & 31 deletions capnp/src/primitive_list.rs
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
use core::{marker};

use crate::traits::{FromPointerReader, FromPointerBuilder, IndexMove, ListIter};
use crate::private::arena::BuilderArena;
use crate::private::layout::{ListReader, ListBuilder, PointerReader, PointerBuilder,
PrimitiveElement};
use crate::Result;
@@ -33,69 +34,69 @@ pub struct Owned<T> {
marker: marker::PhantomData<T>,
}

impl <'a, T> crate::traits::Owned<'a> for Owned<T> where T: PrimitiveElement {
type Reader = Reader<'a, T>;
type Builder = Builder<'a, T>;
impl <'a, T, A: 'a> crate::traits::Owned<'a, A> for Owned<T> where T: PrimitiveElement {
type Reader = Reader<T, &'a A>;
type Builder = Builder<T, &'a mut A>;
}

#[derive(Clone, Copy)]
pub struct Reader<'a, T> where T: PrimitiveElement {
pub struct Reader<T, A> where T: PrimitiveElement {
marker: marker::PhantomData<T>,
reader: ListReader<'a>
reader: ListReader<A>
}

impl <'a, T: PrimitiveElement> Reader<'a, T> {
pub fn new<'b>(reader: ListReader<'b>) -> Reader<'b, T> {
Reader::<'b, T> { reader: reader, marker: marker::PhantomData }
impl <T: PrimitiveElement, A> Reader<T, A> {
pub fn new(reader: ListReader<A>) -> Reader<T, A> {
Reader { reader: reader, marker: marker::PhantomData }
}

pub fn len(&self) -> u32 { self.reader.len() }

pub fn iter(self) -> ListIter<Reader<'a, T>, T>{
pub fn iter(self) -> ListIter<Reader<T, A>, T>{
let l = self.len();
ListIter::new(self, l)
}
}

impl <'a, T: PrimitiveElement> FromPointerReader<'a> for Reader<'a, T> {
fn get_from_pointer(reader: &PointerReader<'a>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a, T>> {
impl <'a, T: PrimitiveElement, A> FromPointerReader<'a, A> for Reader<T, &'a A> {
fn get_from_pointer(reader: &PointerReader<&'a A>, default: Option<&'a [crate::Word]>) -> Result<Reader<T, &'a A>> {
Ok(Reader { reader: reader.get_list(T::element_size(), default)?,
marker: marker::PhantomData })
}
}

impl <'a, T: PrimitiveElement> IndexMove<u32, T> for Reader<'a, T>{
impl <'a, T: PrimitiveElement, A> IndexMove<u32, T> for Reader<T, &'a A> {
fn index_move(&self, index: u32) -> T {
self.get(index)
}
}

impl <'a, T: PrimitiveElement> Reader<'a, T> {
impl <T: PrimitiveElement, A> Reader<T, A> {
pub fn get(&self, index: u32) -> T {
assert!(index < self.len());
PrimitiveElement::get(&self.reader, index)
}
}

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

pub struct Builder<'a, T> where T: PrimitiveElement {
pub struct Builder<T, A> where T: PrimitiveElement {
marker: marker::PhantomData<T>,
builder: ListBuilder<'a>
builder: ListBuilder<A>
}

impl <'a, T> Builder<'a, T> where T: PrimitiveElement {
pub fn new(builder: ListBuilder<'a>) -> Builder<'a, T> {
impl <'a, T, A> Builder<T, &'a mut A> where T: PrimitiveElement, A: BuilderArena {
pub fn new(builder: ListBuilder<&'a mut A>) -> Builder<T, &'a mut A> {
Builder { builder: builder, marker: marker::PhantomData }
}

pub fn len(&self) -> u32 { self.builder.len() }

pub fn into_reader(self) -> Reader<'a, T> {
pub fn into_reader(self) -> Reader<T, &'a A> {
Reader {
marker: marker::PhantomData,
reader: self.builder.into_reader(),
@@ -107,43 +108,43 @@ impl <'a, T> Builder<'a, T> where T: PrimitiveElement {
}
}

impl <'a, T: PrimitiveElement> FromPointerBuilder<'a> for Builder<'a, T> {
fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a, T> {
impl <'a, T: PrimitiveElement, A> FromPointerBuilder<'a, A> for Builder<T, &'a mut A> {
fn init_pointer(builder: PointerBuilder<&'a mut A>, size: u32) -> Builder<T, &'a mut A> {
Builder { builder: builder.init_list(T::element_size(), size),
marker: marker::PhantomData }
}
fn get_from_pointer(builder: PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a, T>> {
fn get_from_pointer(builder: PointerBuilder<&'a mut A>, default: Option<&'a [crate::Word]>) -> Result<Builder<T, &'a mut A>> {
Ok(Builder { builder: builder.get_list(T::element_size(), default)?,
marker: marker::PhantomData })
}
}

impl <'a, T : PrimitiveElement> Builder<'a, T> {
impl <'a, T: PrimitiveElement, A> Builder<T, &'a mut A> {
pub fn get(&self, index: u32) -> T {
assert!(index < self.len());
PrimitiveElement::get_from_builder(&self.builder, index)
}

pub fn reborrow<'b>(&'b self) -> Builder<'b, T> {
pub fn reborrow<'b>(&'b self) -> Builder<T, &'b mut A> {
Builder { .. *self }
}
}

impl <'a, T> crate::traits::SetPointerBuilder<Builder<'a, T>> for Reader<'a, T>
impl <T, A> crate::traits::SetPointerBuilder<Builder<T, A>> for Reader<T, A>
where T: PrimitiveElement
{
fn set_pointer_builder<'b>(pointer: PointerBuilder<'b>,
value: Reader<'a, T>,
canonicalize: bool) -> Result<()> {
fn set_pointer_builder<'b, B>(pointer: PointerBuilder<&'b mut B>,
value: Reader<T, A>,
canonicalize: bool) -> Result<()> {
pointer.set_list(&value.reader, canonicalize)
}
}

impl <'a, T> ::core::iter::IntoIterator for Reader<'a, T>
impl <T, A> ::core::iter::IntoIterator for Reader<T, A>
where T: PrimitiveElement
{
type Item = T;
type IntoIter = ListIter<Reader<'a, T>, Self::Item>;
type IntoIter = ListIter<Reader<T, A>, Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
12 changes: 7 additions & 5 deletions capnp/src/private/capability.rs
Original file line number Diff line number Diff line change
@@ -27,19 +27,20 @@ use crate::MessageSize;
use crate::capability::{Params, Promise, Request, RemotePromise, Results};

pub trait ResponseHook {
fn get<'a>(&'a self) -> crate::Result<any_pointer::Reader<'a>>;
// fn get<'a>(&'a self) -> crate::Result<any_pointer::Reader<'a>>;
}

pub trait RequestHook {
fn get<'a>(&'a mut self) -> any_pointer::Builder<'a>;
// fn get<'a>(&'a mut self) -> any_pointer::Builder<'a>;
fn get_brand(&self) -> usize;
fn send(self: Box<Self>) -> RemotePromise<any_pointer::Owned>;
// fn send(self: Box<Self>) -> RemotePromise<any_pointer::Owned>;
fn tail_send(self: Box<Self>)
-> Option<(u32, crate::capability::Promise<(), crate::Error>, Box<dyn PipelineHook>)>;
}

pub trait ClientHook {
fn add_ref(&self) -> Box<dyn ClientHook>;
/*
fn new_call(&self,
interface_id: u64,
method_id: u16,
@@ -68,6 +69,7 @@ pub trait ClientHook {
/// Repeatedly calls whenMoreResolved() until it returns nullptr.
fn when_resolved(&self) -> Promise<(), crate::Error>;
*/
}

impl Clone for Box<dyn ClientHook> {
@@ -77,15 +79,15 @@ impl Clone for Box<dyn ClientHook> {
}

pub trait ResultsHook {
fn get<'a>(&'a mut self) -> crate::Result<any_pointer::Builder<'a>>;
// fn get<'a>(&'a mut self) -> crate::Result<any_pointer::Builder<'a>>;
fn allow_cancellation(&self);
fn tail_call(self: Box<Self>, request: Box<dyn RequestHook>) -> Promise<(), crate::Error>;
fn direct_tail_call(self: Box<Self>, request: Box<dyn RequestHook>) ->
(crate::capability::Promise<(), crate::Error>, Box<dyn PipelineHook>);
}

pub trait ParamsHook {
fn get<'a>(&'a self) -> crate::Result<crate::any_pointer::Reader<'a>>;
// fn get<'a>(&'a self) -> crate::Result<crate::any_pointer::Reader<'a>>;
}

// Where should this live?
252 changes: 136 additions & 116 deletions capnp/src/private/layout.rs

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions capnp/src/text.rs
Original file line number Diff line number Diff line change
@@ -24,11 +24,12 @@
use core::{convert, str, ops};

use crate::{Error, Result};
use crate::private::arena::{BuilderArena, ReaderArena};

#[derive(Copy, Clone)]
pub struct Owned(());

impl<'a> crate::traits::Owned<'a> for Owned {
impl<'a, A> crate::traits::Owned<'a, A> for Owned {
type Reader = Reader<'a>;
type Builder = Builder<'a>;
}
@@ -43,8 +44,8 @@ pub fn new_reader<'a>(v : &'a [u8]) -> Result<Reader<'a>> {
}
}

impl <'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
fn get_from_pointer(reader: &crate::private::layout::PointerReader<'a>,
impl <'a, A> crate::traits::FromPointerReader<'a, A> for Reader<'a> where A: ReaderArena {
fn get_from_pointer(reader: &crate::private::layout::PointerReader<&'a A>,
default: Option<&'a [crate::Word]>) -> Result<Reader<'a>> {
reader.get_text(default)
}
@@ -110,20 +111,21 @@ impl <'a> convert::AsRef<str> for Builder<'a> {
}
}

impl <'a> crate::traits::FromPointerBuilder<'a> for Builder<'a> {
fn init_pointer(builder: crate::private::layout::PointerBuilder<'a>, size: u32) -> Builder<'a> {
impl <'a, A> crate::traits::FromPointerBuilder<'a, A> for Builder<'a> where A: BuilderArena {
fn init_pointer(builder: crate::private::layout::PointerBuilder<&'a mut A>, size: u32) -> Builder<'a> {
builder.init_text(size)
}
fn get_from_pointer(builder: crate::private::layout::PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a>> {
fn get_from_pointer(builder: crate::private::layout::PointerBuilder<&'a mut A>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a>> {
builder.get_text(default)
}
}

impl <'a> crate::traits::SetPointerBuilder<Builder<'a>> for Reader<'a> {
fn set_pointer_builder<'b>(pointer: crate::private::layout::PointerBuilder<'b>,
value: Reader<'a>,
_canonicalize: bool)
-> Result<()>
fn set_pointer_builder<'b, A>(pointer: crate::private::layout::PointerBuilder<&'b mut A>,
value: Reader<'a>,
_canonicalize: bool)
-> Result<()>
where A: BuilderArena
{
pointer.set_text(value);
Ok(())
42 changes: 22 additions & 20 deletions capnp/src/traits.rs
Original file line number Diff line number Diff line change
@@ -20,33 +20,34 @@
// THE SOFTWARE.

use crate::{Result};
use crate::private::arena::{ReaderArena, BuilderArena};
use crate::private::layout::{CapTable, ListReader, StructReader, StructBuilder, StructSize,
PointerBuilder, PointerReader};

use core::marker::PhantomData;

pub trait FromStructReader<'a> {
fn new(reader: StructReader<'a>) -> Self;
pub trait FromStructReader<'a, A> {
fn new(reader: StructReader<&'a A>) -> Self;
}

pub trait HasStructSize {
fn struct_size() -> StructSize;
}

pub trait IntoInternalStructReader<'a> {
fn into_internal_struct_reader(self) -> StructReader<'a>;
pub trait IntoInternalStructReader<'a, A> {
fn into_internal_struct_reader(self) -> StructReader<&'a A>;
}

pub trait FromStructBuilder<'a> {
fn new(struct_builder: StructBuilder<'a>) -> Self;
pub trait FromStructBuilder<'a, A> {
fn new(struct_builder: StructBuilder<&'a mut A>) -> Self;
}

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

pub trait FromPointerReader<'a> : Sized {
fn get_from_pointer(reader: &PointerReader<'a>, default: Option<&'a [crate::Word]>) -> Result<Self>;
pub trait FromPointerReader<'a, A> : Sized {
fn get_from_pointer(reader: PointerReader<&'a A>, default: Option<&'a [crate::Word]>) -> Result<Self>;
}

/// Associated types hackery that allows us to reason about Cap'n Proto types
@@ -60,27 +61,28 @@ pub trait FromPointerReader<'a> : Sized {
/// cannot be used for anything interesting on its own; the `foo::Owned` type is useful
/// nonetheless as a type parameter, e.g. for a generic container that owns a Cap'n Proto
/// message of type `T: for<'a> capnp::traits::Owned<'a>`.
pub trait Owned<'a> {
type Reader: FromPointerReader<'a> + SetPointerBuilder<Self::Builder>;
type Builder: FromPointerBuilder<'a>;
pub trait Owned<'a, A> {
type Reader: FromPointerReader<'a, A> + SetPointerBuilder<Self::Builder>;
type Builder: FromPointerBuilder<'a, A>;
}

pub trait OwnedStruct<'a> {
type Reader: FromStructReader<'a> + SetPointerBuilder<Self::Builder> + IntoInternalStructReader<'a>;
type Builder: FromStructBuilder<'a> + HasStructSize;
pub trait OwnedStruct<'a, A> {
type Reader: FromStructReader<'a, A> + SetPointerBuilder<Self::Builder> + IntoInternalStructReader<'a, A>;
type Builder: FromStructBuilder<'a, A> + HasStructSize;
}

pub trait Pipelined {
type Pipeline;
}

pub trait FromPointerBuilder<'a> : Sized {
fn init_pointer(builder: PointerBuilder<'a>, length: u32) -> Self;
fn get_from_pointer(builder: PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Self>;
pub trait FromPointerBuilder<'a, A> : Sized {
fn init_pointer(builder: PointerBuilder<&'a mut A>, length: u32) -> Self;
fn get_from_pointer(builder: PointerBuilder<&'a A>, default: Option<&'a [crate::Word]>) -> Result<Self>;
}

pub trait SetPointerBuilder<To> {
fn set_pointer_builder<'a>(builder: PointerBuilder<'a>, from: Self, canonicalize: bool) -> Result<()>;
fn set_pointer_builder<'a, A>(builder: PointerBuilder<&'a mut A>, from: Self, canonicalize: bool) -> Result<()>
where A: BuilderArena;
}

pub trait Imbue<'a> {