-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
portable-atomic-util: Example for Arc<dyn Trait> #143
Comments
Unfortunately, AFAIK, to make this work requires unstable CoerceUnsized trait (rust-lang/rust#18598).
|
let boxed: Box<dyn ...> = Box::new(Foo(42));
let arc: Arc<dyn ...> = Arc::from(boxed); However, the current rustc can omit the allocation of Box if T is Sized (https://godbolt.org/z/s49WxEcan), but unfortunately it cannot omit it if T is Unsized (https://godbolt.org/z/f55oWG1rW), so the generated code will not be the best. EDIT2: As for the // error
let x: Arc<[u32]> = Arc::new([1, 2, 3]);
// ok
let x: Arc<[u32]> = Arc::from([1, 2, 3]); |
This limitation is now documented with the link to the known workaround (a8cc61b). |
Hello,
I am trying to port a crate that uses
Arc<dyn Trait>
for dynamic dispatch to but I could not find a way to do this with this crate.Minimal example:
The error I get:
an 'as' expression can only be used to convert between primitive types or to coerce to a specific trait object
.This example works if
use portable_atomic_util::Arc;
is replaced withuse alloc::sync::Arc;
, though the latter is not available on my target architecture (thumbv6m),.I also tried calling
.into()
and.try_into()
on the Arc, as well as converting the Foo object like this:but those methods work neither on this crate's Arc nor alloc's.
Is there a way to do this using this crate?
The text was updated successfully, but these errors were encountered: