Skip to content

[Question]How to extract generic type values #491

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

Closed
SteveLauC opened this issue Apr 28, 2024 · 5 comments
Closed

[Question]How to extract generic type values #491

SteveLauC opened this issue Apr 28, 2024 · 5 comments

Comments

@SteveLauC
Copy link
Contributor

Hi! Thanks for this amazing project!

I would like to know how I can extract the value of a generic type from its Reader type.

For example, for the following Entry type:

struct Entry(K, V) {
  key @0 :K;
  value @1 :V;
}

I want to implement From<entry::Reader<'_, K, V> for (K, V):

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!()
    }
}

As you can see, I can access their Reader types, but how can I extra their values, i.e., values of type K and V, thanks!

@dwrensha
Copy link
Member

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>;
}
.

@SteveLauC
Copy link
Contributor Author

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>

@SteveLauC
Copy link
Contributor Author

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.

@dwrensha
Copy link
Member

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

@SteveLauC
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants