Skip to content

Commit

Permalink
Remove bounds on Config trait that aren't strictly necessary (parityt…
Browse files Browse the repository at this point in the history
…ech#389)

* Use Derivative to skip bounds on T when they aren't necessary, and remove unnecessary bounds on Config

* loosen a couple more derive bounds

* Use PhantomDataSendSync to avoid accidentally removing Send+Sync bounds
  • Loading branch information
jsdw authored and 0623forbidden committed Feb 15, 2022
1 parent dec67e8 commit 415b018
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 99 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate/", branch = "maste
sp-version = { package = "sp-version", git = "https://github.com/paritytech/substrate/", branch = "master" }

frame-metadata = "14.0.0"
derivative = "2.2.0"

[dev-dependencies]
sp-arithmetic = { git = "https://github.com/paritytech/substrate/", branch = "master", default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
Config,
Metadata,
};
use derivative::Derivative;
use std::sync::Arc;

/// ClientBuilder for constructing a Client.
Expand Down Expand Up @@ -111,7 +112,8 @@ impl ClientBuilder {
}

/// Client to interface with a substrate node.
#[derive(Clone)]
#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
pub struct Client<T: Config> {
rpc: Rpc<T>,
genesis_hash: T::Hash,
Expand Down
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use sp_runtime::traits::{
};

/// Runtime types.
pub trait Config: Clone + Default + Sized + Send + Sync + 'static {
// Note: the 'static bound isn't strictly required, but currently deriving TypeInfo
// automatically applies a 'static bound to all generic types (including this one),
// and so until that is resolved, we'll keep the (easy to satisfy) constraint here.
pub trait Config: 'static {
/// Account index (aka nonce) type. This stores the number of previous
/// transactions associated with a sender account.
type Index: Parameter + Member + Default + AtLeast32Bit + Copy + scale_info::TypeInfo;
Expand Down Expand Up @@ -83,8 +86,9 @@ pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug {}
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug {}

/// Default set of commonly used types by Substrate runtimes.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DefaultConfig;
// Note: We only use this at the type level, so it should be impossible to
// create an instance of it.
pub enum DefaultConfig {}

impl Config for DefaultConfig {
type Index = u32;
Expand Down
32 changes: 15 additions & 17 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

use codec::{
Codec,
Compact,
Decode,
Encode,
Error as CodecError,
Input,
};
use std::marker::PhantomData;

use crate::{
metadata::{
EventMetadata,
Expand All @@ -33,8 +23,18 @@ use crate::{
Error,
Event,
Metadata,
PhantomDataSendSync,
Phase,
};
use codec::{
Codec,
Compact,
Decode,
Encode,
Error as CodecError,
Input,
};
use derivative::Derivative;
use scale_info::{
TypeDef,
TypeDefPrimitive,
Expand Down Expand Up @@ -69,16 +69,14 @@ impl RawEvent {
}

/// Events decoder.
#[derive(Debug, Clone)]
pub struct EventsDecoder<T> {
#[derive(Derivative)]
#[derivative(Clone(bound = ""), Debug(bound = ""))]
pub struct EventsDecoder<T: Config> {
metadata: Metadata,
marker: PhantomData<T>,
marker: PhantomDataSendSync<T>,
}

impl<T> EventsDecoder<T>
where
T: Config,
{
impl<T: Config> EventsDecoder<T> {
/// Creates a new `EventsDecoder`.
pub fn new(metadata: Metadata) -> Self {
Self {
Expand Down
Loading

0 comments on commit 415b018

Please sign in to comment.