Comments (6)
Note that instead of set_foo(x.as_str().into())
, you can do set_foo(x[..].into())
, which is a bit shorter.
from capnproto-rust.
Adding separate setter methods for capnp::Text::Reader
and str
could maybe work. We would need to figure out:
- naming -- I don't like
raw_
because that already means "raw pointer" elsewhere - how it would interact with generics -- if a field has a generic type
T
, then does that field now need two different setters, becauseT
might be instantiated totext::Owned
?
Alternatively, we could consider reviving #421, which would make pointer setters accept any type that implements SetPointerBuilder
.
I'm reluctant to do either of these because they add more complexity, but maybe the potential ergonomic gains for downstream users outweigh the cost.
from capnproto-rust.
Alternative naming suggestion: for a text field foo
and text reader r
, fd_foo().set(r)
. fd_foo
returns a TextField
.
This aligns with another proposal I was planning to make, which is to reduce the boilerplate of building lists of structs, by enabling writing, for a field bars
of Capnp type List(Bar)
and some v
of native Rust type Vec<MyBar>
, something like fd_bars().from_iterable(v, |bar_cpn, bar| { /* Initialize the bar_cpn from the bar */ })
. The essence of a Field is that, given a length, it can return a Builder: fd_bars().init(len)
would return a struct_list::Builder<bar::Owned>
. Conveniences such as from_iterable
can be built on top of that.
from capnproto-rust.
FYI: I decided to vendor capnproto-rust, so I am not blocked on this issue being resolved here. I reverted the text setters to taking &str
verifast@01871b5 . I also changed the init_foo
methods for initializing lists to taking a usize
length instead of u32
, and the list get
method to taking a usize
index. Furthermore, I generate a fill_foo
method for each field foo
of type list-of-struct, that looks like this:
#[inline]
pub fn fill_args<E, F>(&mut self, elems: E, body: F)
where F: FnMut(crate::vf_mir_capnp::body::basic_block::operand::Builder<'_>, E::Item), E: IntoIterator, E::IntoIter: ExactSizeIterator {
<::capnp::struct_list::Builder<'_,crate::vf_mir_capnp::body::basic_block::operand::Owned>>::fill(self.builder.reborrow().get_pointer_field(1), elems, body)
}
which uses a new fill
associated function in struct_list::Builder
:
impl<'a, T> Builder<'a, T>
where
T: crate::traits::OwnedStruct + 'a,
{
pub fn fill<E, F>(builder: PointerBuilder<'a>, elems: E, mut body: F)
where F: FnMut(T::Builder<'_>, E::Item), E: IntoIterator, E::IntoIter: ExactSizeIterator
{
let iter = elems.into_iter();
let mut list_builder = Self::init_pointer_with_usize_length(builder, iter.len());
for (idx, elem) in iter.enumerate() {
let elem_cpn = list_builder.reborrow().get(idx);
body(elem_cpn, elem);
}
}
}
Commit: verifast/verifast@60e9ca4#diff-fe25a34e136e01802f5f365abad8bf12d11c18a922ffd283bed2c3af52eb1e35
I also did something similar for text lists: I generate a method fill_foo
for each field of type List(Text)
:
#[inline]
pub fn fill_arg_names<E>(&mut self, elems: E)
where E: IntoIterator, E::IntoIter: ExactSizeIterator, E::Item: AsRef<str> {
<::capnp::text_list::Builder<'_>>::fill(self.builder.reborrow().get_pointer_field(3), elems)
}
which uses a new fill
associated function on text_list::Builder
:
impl<'a> Builder<'a> {
pub fn fill<E>(builder: PointerBuilder<'a>, elems: E)
where E: IntoIterator, E::IntoIter: ExactSizeIterator, E::Item: AsRef<str>
{
let iter = elems.into_iter();
let mut list_builder = Self::init_pointer_with_usize_length(builder, iter.len());
for (idx, elem) in iter.enumerate() {
list_builder.set(idx, elem.as_ref());
}
}
}
Commit: verifast/verifast@68b70c6
from capnproto-rust.
I revived #421 as #472. I think this works well, but unfortunately would re-break the code of everyone who has already updated to capnp-v0.18.
from capnproto-rust.
Closing, because I've landed #472.
Thanks for pushing on this!
from capnproto-rust.
Related Issues (20)
- Primitive list panicks if the list type is `Void` HOT 1
- Null values for non active union members HOT 3
- Switch Allocator to act more like an owned arena HOT 8
- Reader fails with out of bounds pointer error when reading from compressed stream HOT 1
- calculator example fails with java server: Cannot read capability pointer without capTable HOT 2
- c++.capnp compiles to unusable module name HOT 6
- How to send generated readers across threads HOT 3
- From bytes to TypedReader HOT 6
- Allowing construction of NoAllocSliceSegments with buffer and NoAllocBufferSegmentType HOT 7
- Kudos to builders of this Rust library - Every single one of you. HOT 3
- [Question]How to extract generic type values HOT 5
- capnp_rpc does not work on monoio HOT 4
- Dynamic parsing of fields may panic HOT 1
- traits.ListIter incorrectly implementing ExactSizeIterator HOT 1
- oc
- Using same schema file for both Rust and C++ HOT 1
- doc: read_message_from_flat_slice_no_alloc docstring bad autocomplete HOT 1
- Making rust capn proto fully async HOT 2
- Is there any reason why Rc<RefCell> is used compared to Arc<Mutex>?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from capnproto-rust.