Skip to content

Sharing Rust structs in foreign libraries / Writing complex wrapper classes #3634

Discussion options

You must be logged in to vote

You can see the "foreign" structs are:

Those structures do not support shared ownership (not even in Rust) and hence you cannot use that on language level. You would would have to introduce something like Arc or our own Py smart pointer into the mix so that you would store e.g. HashMap<String, Vec<Arc<Bar>> or HashMap<String, Vec<Py<Bar>>.

If you really cannot modify these structures because they are defined by a foreign crate, I would suggest to use shared ownership only conceptually but not actually, for example do not store Bar within BarWrapper, but the necessary data to access the bar, e.g.

struct BarWrapper {
  foo: Py<FooWrapper>,
  key: String,
  index: usize,
}

impl BarWrapper {

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@warriorstar-orion
Comment options

@adamreichold
Comment options

Answer selected by warriorstar-orion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants