Skip to content
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

Generify Account::client over Rc, Arc #707

Merged
merged 6 commits into from
Mar 14, 2022

Conversation

cycraig
Copy link
Contributor

@cycraig cycraig commented Mar 12, 2022

Description of change

This changes the Account and its related structs to be generic over Arc<Client> and Rc<Client> so that we can use WasmClient directly without needing to clone the internal Client.

Unfortunately, to make a long story short: we cannot implement a higher kinded type for Arc/Rc over unsized types, namely dyn Storage in the Account, due to the lack of CoerceUnsized in stable Rust.

I believe that without implementing CoerceUnsized for whatever SharedPtrUnsized trait we come up with, we will be unable to circumvent the type-mismatch error when trying to pass a concrete instance like Arc<MemStore>.
Example:

AccountSetup::new(
	Arc::new(MemStore::new()),
	Arc::new(
	  ClientBuilder::new()
		.network(network)
		.node_sync_disabled()
		.build()
		.await
		.unwrap(),
	),
	AccountConfig::new(),
)
error[E0271]: type mismatch resolving `<account::account::ArcPtr as account::account::SharedPtrUnsized<(dyn storage::traits::Storage + 'static)>>::Ptr == std::sync::Arc<storage::memstore::MemStore>`
  --> identity-account\src\tests\updates.rs:39:3
   |
39 | /   AccountSetup::new(
40 | |     Arc::new(MemStore::new()),
41 | |     Arc::new(
42 | |       ClientBuilder::new()
...  |
49 | |     AccountConfig::new().testmode(true),
50 | |   )
   | |___^ type mismatch resolving `<account::account::ArcPtr as account::account::SharedPtrUnsized<(dyn storage::traits::Storage + 'static)>>::Ptr == std::sync::Arc<storage::memstore::MemStore>`
   |
note: expected this to be `std::sync::Arc<(dyn storage::traits::Storage + 'static)>`
  --> identity-account\src\account\account.rs:72:14
   |
72 |   type Ptr = Arc<T>;
   |              ^^^^^^
   = note: expected struct `std::sync::Arc<(dyn storage::traits::Storage + 'static)>`
              found struct `std::sync::Arc<storage::memstore::MemStore>`

The archery crate also suffers from this limitation. There is no indication that CoerceUnsized will be stabilized any time soon.

https://stackoverflow.com/questions/69500407/trait-object-causing-type-mismatch
https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html

Given the above, I chose to implement the same "poor-man's higher kinded type" approach I used for the Resolver in #594, which only works for Arc<Client>/Rc<Client> due to the same issue.

Type of change

  • Bug fix (a non-breaking change which fixes an issue)
  • Enhancement (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Fix

How the change has been tested

Tests and examples pass locally.

Change checklist

  • I have followed the contribution guidelines for this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@cycraig cycraig added Added A new feature that requires a minor release. Part of "Added" section in changelog Rust Related to the core Rust code. Becomes part of the Rust changelog. labels Mar 12, 2022
@cycraig cycraig force-pushed the feat/account-shared-ptr branch from fc54053 to b1b843b Compare March 12, 2022 12:53
@cycraig cycraig marked this pull request as ready for review March 12, 2022 19:27
@cycraig
Copy link
Contributor Author

cycraig commented Mar 12, 2022

Windows build-and-test job fails due to scoop, fixed on dev already and in the epic branch with #708, pending review.

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

bindings/wasm/src/account/wasm_account/account.rs Outdated Show resolved Hide resolved
@cycraig cycraig merged commit 874d77d into epic/wasm-bindings-account Mar 14, 2022
@cycraig cycraig deleted the feat/account-shared-ptr branch March 14, 2022 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Added A new feature that requires a minor release. Part of "Added" section in changelog Rust Related to the core Rust code. Becomes part of the Rust changelog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants