Giter VIP home page Giter VIP logo

Comments (5)

dwrensha avatar dwrensha commented on July 20, 2024

There are no useful values of types K and V. The only way to interact with Cap'n Proto data is through Reader and Builder types. See this comment:

/// A trait to encode relationships between readers and builders.
///
/// If `Foo` is a Cap'n Proto struct and `Bar` is a Rust-native struct, then
/// `foo::Reader<'a>` is to `foo::Owned` as `&'a Bar` is to `Bar`, and
/// `foo::Builder<'a>` is to `foo::Owned` as `&'a mut Bar` is to `Bar`.
/// The relationship is formalized by an `impl capnp::traits::Owned for foo::Owned`.
/// Because Cap'n Proto struct layout differs from Rust struct layout, a `foo::Owned` value
/// 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: capnp::traits::Owned`.
pub trait Owned: crate::introspect::Introspect {
type Reader<'a>: FromPointerReader<'a> + SetterInput<Self>;
type Builder<'a>: FromPointerBuilder<'a>;
}
.

from capnproto-rust.

SteveLauC avatar SteveLauC commented on July 20, 2024
impl<K, V> From<entry::Reader<'_, K, V>> for (K, V)
where
    K: capnp::traits::Owned,
    V: capnp::traits::Owned,
{
    fn from(value: crate::map_capnp::entry::Reader<'_, K, V>) -> Self {
        let key: <K as Owned>::Reader = value.get_key().unwrap();
        let value: <V as Owned>::Reader = value.get_value().unwrap();

        
        todo!()
    }
}

Thanks for the quick response, so if I understand correctly, the generic types K and V defined in the above code are CapN types, I cannot convert Entry to a CapN (K, V) pair, instead, I should define 2 extra Rust K V types:

impl <CapnK, CapnV, RustK, RustV> From<Entry<'_, CapnK, CapnV>> for (RustK, RustV) 

and there should be a trait bound that one can convert a <CapnK as Owned>::Reader to a RustK, something like:

where RustK: From<<CapnK as Owned>::Reader>

from capnproto-rust.

SteveLauC avatar SteveLauC commented on July 20, 2024

Hi, I am curious if the approach presented in my last comment is feasible, it seems there is no general abstraction (traits or methods) to extract the RustK value from the CapnK reader type, I have to know the concrete reader type (struct_list::Reader, dynamic_value::Reader, etc) and call the corresponding methods to get the value.

from capnproto-rust.

dwrensha avatar dwrensha commented on July 20, 2024

the generic types K and V defined in the above code are CapN types

Correct. Generic type in Cap'n Proto messages can only be instantiated with Cap'n Proto types.

it seems there is no general abstraction (traits or methods) to extract the RustK value from the CapnK reader type

That's correct. In #157 there was some work towards proc macros that convert between capnproto and rust-native types. That got turned into its own repo (which I have not looked at in depth): https://github.com/aikalant/capnp_conv

from capnproto-rust.

SteveLauC avatar SteveLauC commented on July 20, 2024

That's correct. In #157 there was some work towards proc macros that convert between capnproto and rust-native types. That got turned into its own repo (which I have not looked at in depth): https://github.com/aikalant/capnp_conv

Yeah, that crate defines 2 helper traits like:

pub trait Writable {
    type OwnedType: capnp::traits::Owned;

    fn write(&self, builder: <Self::OwnedType as Owned>::Builder<'_>);
}

pub trait Readable
where
    Self: Sized,
{
    type OwnedType: capnp::traits::Owned;

    fn read(reader: <Self::OwnedType as Owned>::Reader<'_>) -> Result<Self>;
}

so that one can do <RustK as Readable>::read(CapnK_reader) to extract the value, though I didn't take a deep look either.

Thanks for the response!

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.