Feddit
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
SorteKanin@feddit.dk to Rust@programming.dev · 18 days ago

Downcasting Arcs in Rust

ashdnazg.github.io

external-link
message-square
0
link
fedilink
1
external-link

Downcasting Arcs in Rust

ashdnazg.github.io

SorteKanin@feddit.dk to Rust@programming.dev · 18 days ago
message-square
0
link
fedilink
The Arc type is Rust’s thread-safe smart pointer, and like other pointer types in Rust (e.g., Box), one can use the as keyword1 to cast an Arc into an Arc, as long as MyTrait is dyn compatible. What about casting it back from Arc to Arc? Well, here the plot thickens a bit. We can’t use the as keyword here, because that operation is inherently unsafe. An arbitrary Arc might not hold a MyStruct in it, but some other object that implements MyTrait, and trying to cast it into the wrong type would result in undefined behaviour. But the fact that something is unsafe doesn’t mean it should be impossible, or even undesirable. An unsized coercion if you want the fancy term. ↩
alert-triangle
You must log in or register to comment.

Rust@programming.dev

rust@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !rust@programming.dev

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1 user / day
  • 7 users / week
  • 16 users / month
  • 50 users / 6 months
  • 3 local subscribers
  • 8.24K subscribers
  • 1.36K Posts
  • 4.91K Comments
  • Modlog
  • mods:
  • snowe@programming.dev
  • Ategon@programming.dev
  • EdTheLegendary@programming.dev
  • torcherist@programming.dev
  • BE: 0.19.11
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org