Storing unboxed trait objects in Rust
guiand.xyz
Storing unboxed trait objects in Rust
1–10 of 25 posts
Re: Storing unboxed trait objects in Rust
#2Re: Storing unboxed trait objects in Rust
#3The closest analog to Objective-C's @protocols in Rust are traits. I would love to be able to transliterate the following Objective-C:
@protocol SomeProtocol
// methods
@end
void demo() {
// Just a demo of how to use SomeProtocol with a var:
id variable = nil;
}
To the following Rust: #[objrs(protocol)]
trait SomeProtocol {
// methods
}
fn demo() {
// Just a demo of how to use SomeProtocol with a var:
let variable: *mut Id = 0 as *mut _;
}
// The following is provided by objrs:
struct Id(core::marker::PhantomData, Opaque);
extern "C" {
type Opaque;
}
Unfortunately, RFC 255[2] breaks this. This fails because SomeProtocol is not object safe. Trait objects in Rust can be pretty frustrating in Rust, as this post illustrates.I've had to workaround this in objrs by doing something like the following:
#[objrs(protocol)]
trait SomeProtocol {
// methods
}
// The macro above generates stuff kinda like this:
struct SomeProtocolId;
impl SomeProtocol for SomeProtocolId {}
impl SomeProtocol for Id {}
fn demo() {
// Now you have to know when to use SomeProtocol vs SomeProtocolId:
let variable: *mut Id = 0 as *mut _;
}
I'm going to see if there are some places where this blog post might simplify some things in my code... it's got some interesting ideas.[1]: https://gitlab.com/objrs/objrs
[2]: https://github.com/rust-lang/rfcs/blob/master/text/0255-obje...
Re: Storing unboxed trait objects in Rust
#4TL;DR this relies on unstable internal details; there’s a proposal to add an API to let you do this kind of thing but it hasn’t been accepted yet.
Re: Storing unboxed trait objects in Rust
#5Re: Storing unboxed trait objects in Rust
#6This post reminds me of a problem I've hit with my Objective-Rust project[1] (Rust code that's interoperable with Objective-C), to which I haven't found a good solution. The closest analog to Objective-C's @protocols in Rust are traits. I would love to be able to transliterate the following Objective-C: @protocol SomeProtocol // methods @end void demo() { // Just a demo of how to use SomeProtocol with a var: id varia…
Re: Storing unboxed trait objects in Rust
#7IMHO, Rust just should take size of the largest implementation of Animal for `impl Animal`, and viola — we can make them static. No heap is good for embedded.
Re: Storing unboxed trait objects in Rust
#8IMHO, Rust just should take size of the largest implementation of Animal for `impl Animal`, and viola — we can make them static. No heap is good for embedded.
Re: Storing unboxed trait objects in Rust
#9IMHO, Rust just should take size of the largest implementation of Animal for `impl Animal`, and viola — we can make them static. No heap is good for embedded.
Note that it's quite possible for a trait to have infinitely many implementations in any given program. For example, Debug is implemented by u64, Box , Box >, ..., and so on. While these happen to have the same size, it's not hard to imagine a situation where this is not the case.
Re: Storing unboxed trait objects in Rust
#10This post reminds me of a problem I've hit with my Objective-Rust project[1] (Rust code that's interoperable with Objective-C), to which I haven't found a good solution. The closest analog to Objective-C's @protocols in Rust are traits. I would love to be able to transliterate the following Objective-C: @protocol SomeProtocol // methods @end void demo() { // Just a demo of how to use SomeProtocol with a var: id varia…
https://github.com/Centril/rfc-trait-parametric-polymorphism...
After being created 8 months ago, it's sort of inactive, but hopefully that'll change once the compiler's feature-implementation backlog starts catching up – in particular, once "chalk in rustc" becomes a reality.