Giter VIP home page Giter VIP logo

Comments (6)

dwrensha avatar dwrensha commented on September 25, 2024

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.

dwrensha avatar dwrensha commented on September 25, 2024

Adding separate setter methods for capnp::Text::Reader and str could maybe work. We would need to figure out:

  1. naming -- I don't like raw_ because that already means "raw pointer" elsewhere
  2. how it would interact with generics -- if a field has a generic type T, then does that field now need two different setters, because T might be instantiated to text::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.

btj avatar btj commented on September 25, 2024

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.

btj avatar btj commented on September 25, 2024

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.

dwrensha avatar dwrensha commented on September 25, 2024

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.

dwrensha avatar dwrensha commented on September 25, 2024

Closing, because I've landed #472.
Thanks for pushing on this!

from capnproto-rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.