You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue was labelled with: A-an-interesting-project, E-hard in the Rust repository
<kmc> i want a world where I can send a Box<Encodable + FnOnce(A) -> B> across the network and call it on the other end <Yurume> kmc: with a built-in AEAD protocol...
I think it’s possible to do this without making any changes to how trait objects are handled. It also doesn't require any form of trait polymorphism.
We would start with a magical built-in trait (call it TyConst for now):
// This represents a mapping from a type T to some DATA: Self.// (The ordering here is important to avoid causing orphan issues.)pubtraitTyConst<T: ?Sized + 'static>:Sized{constDATA:Self;}// Get the key associated with TyConst<T> for D. Uses of this function// determine what goes into the type constant table for D.pubfnget_ty_const_key<D:TyConst<T>,T: ?Sized + 'static>() -> usize;// Get the DATA field in the impl for the type that matches the given key.// If no such impl is found, returns None.pubfnget_ty_const<D>(key:usize) -> Option<D>;
There is no way to define get_ty_const portably in today’s Rust. In fact, the function get_ty_const can't be generated until the entire program has been compiled because it requires knowing every instantiation of get_ty_const_key in the entire program!
During translation phase, the compiler would have to sift through all instantiations of get_ty_const_key and create a global table [D; _] for every D. In each table, an entry containing the matching DATA is inserted for every instantiation of get_ty_const_key<D, T>. The get_ty_const_key function is set to return the index of this entry, whereas get_ty_const simply retrieves the entry at index key from the table that corresponds to D.
Using this one trait, and with the help of a serialization package like serde, we can build a library that decodes arbitrary function-like objects:
On the users’ side, if they want to support deserialization for a custom data type Foo, they would write:
#[derive(Encodable,Decodable)]structFoo(Vec<u8>);implEncodableFnforFoo{fncall(self:Box<Self>){println!("Foo has been called with {:?}!",self.0);}}
Edit: refine idea a bit more and remove unnecessary dependence on TypeId.
Edit 2: one problem: for an incomplete program (i.e. shared lib) how would get_ty_const_key() be defined?
Edit 3: Okay here's a concrete implementation of the above idea using stable Rust plus a lot of highly questionable low-level hacks: https://crates.io/crates/detrojt
Activity
Rufflewind commentedon Mar 13, 2017
I think it’s possible to do this without making any changes to how trait objects are handled. It also doesn't require any form of trait polymorphism.
We would start with a magical built-in trait (call it
TyConstfor now):There is no way to define
get_ty_constportably in today’s Rust. In fact, the functionget_ty_constcan't be generated until the entire program has been compiled because it requires knowing every instantiation ofget_ty_const_keyin the entire program!During translation phase, the compiler would have to sift through all instantiations of
get_ty_const_keyand create a global table[D; _]for everyD. In each table, an entry containing the matchingDATAis inserted for every instantiation ofget_ty_const_key<D, T>. Theget_ty_const_keyfunction is set to return the index of this entry, whereasget_ty_constsimply retrieves the entry at indexkeyfrom the table that corresponds toD.Using this one trait, and with the help of a serialization package like serde, we can build a library that decodes arbitrary function-like objects:
On the users’ side, if they want to support deserialization for a custom data type
Foo, they would write:Edit: refine idea a bit more and remove unnecessary dependence on
TypeId.Edit 2: one problem: for an incomplete program (i.e. shared lib) how would
get_ty_const_key()be defined?Edit 3: Okay here's a concrete implementation of the above idea using stable Rust plus a lot of highly questionable low-level hacks: https://crates.io/crates/detrojt
alecmocatta commentedon Nov 5, 2019
I've taken a stab at this with rust-lang/rust#66113