From d94c4e3357821dc2cad7fdce62731ae4e4fd2c25 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Wed, 8 Jan 2020 13:21:23 +0100 Subject: [PATCH] Use `Self` instead of type name --- common/src/truffle.rs | 6 +++--- common/src/truffle/bytecode.rs | 2 +- generate/src/contract.rs | 2 +- generate/src/lib.rs | 6 +++--- src/contract.rs | 8 +++---- src/contract/deploy.rs | 22 ++++++++----------- src/contract/method.rs | 39 +++++++++++++++------------------- src/errors.rs | 10 ++++----- src/future.rs | 4 ++-- src/test/transport.rs | 2 +- src/transaction.rs | 26 +++++++++++------------ src/transport.rs | 2 +- 12 files changed, 60 insertions(+), 69 deletions(-) diff --git a/common/src/truffle.rs b/common/src/truffle.rs index e1d368cc..dd427ef7 100644 --- a/common/src/truffle.rs +++ b/common/src/truffle.rs @@ -32,7 +32,7 @@ pub struct Artifact { impl Artifact { /// Creates an empty artifact instance. - pub fn empty() -> Artifact { + pub fn empty() -> Self { Artifact { contract_name: String::new(), abi: Abi { @@ -49,7 +49,7 @@ impl Artifact { } /// Parse a truffle artifact from JSON. - pub fn from_json(json: S) -> Result + pub fn from_json(json: S) -> Result where S: AsRef, { @@ -58,7 +58,7 @@ impl Artifact { } /// Loads a truffle artifact from disk. - pub fn load

(path: P) -> Result + pub fn load

(path: P) -> Result where P: AsRef, { diff --git a/common/src/truffle/bytecode.rs b/common/src/truffle/bytecode.rs index 90c126b7..82bba58d 100644 --- a/common/src/truffle/bytecode.rs +++ b/common/src/truffle/bytecode.rs @@ -16,7 +16,7 @@ pub struct Bytecode(String); impl Bytecode { /// Read hex bytecode representation from a string slice. - pub fn from_hex_str(s: S) -> Result + pub fn from_hex_str(s: S) -> Result where S: AsRef, { diff --git a/generate/src/contract.rs b/generate/src/contract.rs index e7e17706..896c3b9d 100644 --- a/generate/src/contract.rs +++ b/generate/src/contract.rs @@ -27,7 +27,7 @@ struct Context { } impl Context { - fn from_args(args: &Args) -> Result { + fn from_args(args: &Args) -> Result { let artifact_path = { let full_path = fs::canonicalize(&args.artifact_path)?; Literal::string(&full_path.to_string_lossy()) diff --git a/generate/src/lib.rs b/generate/src/lib.rs index d175d096..82fa7e7d 100644 --- a/generate/src/lib.rs +++ b/generate/src/lib.rs @@ -25,7 +25,7 @@ pub(crate) struct Args { impl Args { /// Creates a new builder given the path to a contract's truffle artifact /// JSON file. - pub fn new

(artifact_path: P) -> Args + pub fn new

(artifact_path: P) -> Self where P: AsRef, { @@ -46,7 +46,7 @@ pub struct Builder { impl Builder { /// Creates a new builder given the path to a contract's truffle artifact /// JSON file. - pub fn new

(artifact_path: P) -> Builder + pub fn new

(artifact_path: P) -> Self where P: AsRef, { @@ -57,7 +57,7 @@ impl Builder { /// Sets the crate name for the runtime crate. This setting is usually only /// needed if the crate was renamed in the Cargo manifest. - pub fn with_runtime_crate_name(mut self, name: S) -> Builder + pub fn with_runtime_crate_name(mut self, name: S) -> Self where S: AsRef, { diff --git a/src/contract.rs b/src/contract.rs index 2e0dacb6..e14b65cd 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -35,7 +35,7 @@ impl Instance { /// /// Note that this does not verify that a contract with a matchin `Abi` is /// actually deployed at the given address. - pub fn at(web3: Web3, abi: Abi, address: Address) -> Instance { + pub fn at(web3: Web3, abi: Abi, address: Address) -> Self { Instance { web3, abi, @@ -49,7 +49,7 @@ impl Instance { /// /// Note that this does not verify that a contract with a matchin `Abi` is /// actually deployed at the given address. - pub fn deployed(web3: Web3, artifact: Artifact) -> DeployedFuture> { + pub fn deployed(web3: Web3, artifact: Artifact) -> DeployedFuture { DeployedFuture::from_args(web3, artifact) } @@ -60,7 +60,7 @@ impl Instance { web3: Web3, artifact: Artifact, params: P, - ) -> Result>, DeployError> + ) -> Result, DeployError> where P: Tokenize, { @@ -74,7 +74,7 @@ impl Instance { artifact: Artifact, params: P, libraries: I, - ) -> Result>, DeployError> + ) -> Result, DeployError> where P: Tokenize, I: Iterator, diff --git a/src/contract/deploy.rs b/src/contract/deploy.rs index 1a7d7fc6..a8a6c42b 100644 --- a/src/contract/deploy.rs +++ b/src/contract/deploy.rs @@ -53,7 +53,7 @@ where { /// Construct a new future that resolves when a deployed contract is located /// from a `web3` provider and artifact data. - pub fn from_args(web3: Web3, artifact: Artifact) -> DeployedFuture { + pub fn from_args(web3: Web3, artifact: Artifact) -> Self { let net = web3.net(); DeployedFuture { args: Some((web3.into(), artifact)), @@ -125,11 +125,7 @@ where { /// Create a new deploy builder from a `web3` provider, artifact data and /// deployment (constructor) parameters. - pub fn new

( - web3: Web3, - artifact: Artifact, - params: P, - ) -> Result, DeployError> + pub fn new

(web3: Web3, artifact: Artifact, params: P) -> Result where P: Tokenize, { @@ -159,35 +155,35 @@ where /// Specify the signing method to use for the transaction, if not specified /// the the transaction will be locally signed with the default user. - pub fn from(mut self, value: Account) -> DeployBuilder { + pub fn from(mut self, value: Account) -> Self { self.tx = self.tx.from(value); self } /// Secify amount of gas to use, if not specified then a gas estimate will /// be used. - pub fn gas(mut self, value: U256) -> DeployBuilder { + pub fn gas(mut self, value: U256) -> Self { self.tx = self.tx.gas(value); self } /// Specify the gas price to use, if not specified then the estimated gas /// price will be used. - pub fn gas_price(mut self, value: U256) -> DeployBuilder { + pub fn gas_price(mut self, value: U256) -> Self { self.tx = self.tx.gas_price(value); self } /// Specify what how much ETH to transfer with the transaction, if not /// specified then no ETH will be sent. - pub fn value(mut self, value: U256) -> DeployBuilder { + pub fn value(mut self, value: U256) -> Self { self.tx = self.tx.value(value); self } /// Specify the nonce for the transation, if not specified will use the /// current transaction count for the signing account. - pub fn nonce(mut self, value: U256) -> DeployBuilder { + pub fn nonce(mut self, value: U256) -> Self { self.tx = self.tx.nonce(value); self } @@ -195,7 +191,7 @@ where /// Specify the number of confirmations to wait for when confirming the /// transaction, if not specified will wait for the transaction to be mined /// without any extra confirmations. - pub fn confirmations(mut self, value: usize) -> DeployBuilder { + pub fn confirmations(mut self, value: usize) -> Self { self.tx = self.tx.confirmations(value); self } @@ -233,7 +229,7 @@ where D: Deploy, { /// Create an instance from a `DeployBuilder`. - pub fn from_builder(builder: DeployBuilder) -> DeployFuture { + pub fn from_builder(builder: DeployBuilder) -> Self { DeployFuture { args: Some((builder.web3.into(), builder.abi)), send: builder.tx.send(), diff --git a/src/contract/method.rs b/src/contract/method.rs index b17c5838..16cec904 100644 --- a/src/contract/method.rs +++ b/src/contract/method.rs @@ -45,12 +45,7 @@ pub struct MethodBuilder { impl MethodBuilder { /// Creates a new builder for a transaction. - pub fn new( - web3: Web3, - function: Function, - address: Address, - data: Bytes, - ) -> MethodBuilder { + pub fn new(web3: Web3, function: Function, address: Address, data: Bytes) -> Self { MethodBuilder { web3: web3.clone(), function, @@ -60,7 +55,7 @@ impl MethodBuilder { } /// Apply method defaults to this builder. - pub fn with_defaults(mut self, defaults: &MethodDefaults) -> MethodBuilder { + pub fn with_defaults(mut self, defaults: &MethodDefaults) -> Self { self.tx.from = self.tx.from.or_else(|| defaults.from.clone()); self.tx.gas = self.tx.gas.or(defaults.gas); self.tx.gas_price = self.tx.gas_price.or(defaults.gas_price); @@ -69,35 +64,35 @@ impl MethodBuilder { /// Specify the signing method to use for the transaction, if not specified /// the the transaction will be locally signed with the default user. - pub fn from(mut self, value: Account) -> MethodBuilder { + pub fn from(mut self, value: Account) -> Self { self.tx = self.tx.from(value); self } /// Secify amount of gas to use, if not specified then a gas estimate will /// be used. - pub fn gas(mut self, value: U256) -> MethodBuilder { + pub fn gas(mut self, value: U256) -> Self { self.tx = self.tx.gas(value); self } /// Specify the gas price to use, if not specified then the estimated gas /// price will be used. - pub fn gas_price(mut self, value: U256) -> MethodBuilder { + pub fn gas_price(mut self, value: U256) -> Self { self.tx = self.tx.gas_price(value); self } /// Specify what how much ETH to transfer with the transaction, if not /// specified then no ETH will be sent. - pub fn value(mut self, value: U256) -> MethodBuilder { + pub fn value(mut self, value: U256) -> Self { self.tx = self.tx.value(value); self } /// Specify the nonce for the transation, if not specified will use the /// current transaction count for the signing account. - pub fn nonce(mut self, value: U256) -> MethodBuilder { + pub fn nonce(mut self, value: U256) -> Self { self.tx = self.tx.nonce(value); self } @@ -105,7 +100,7 @@ impl MethodBuilder { /// Specify the number of confirmations to wait for when confirming the /// transaction, if not specified will wait for the transaction to be mined /// without any extra confirmations. - pub fn confirmations(mut self, value: usize) -> MethodBuilder { + pub fn confirmations(mut self, value: usize) -> Self { self.tx = self.tx.confirmations(value); self } @@ -133,7 +128,7 @@ pub struct MethodFuture { impl MethodFuture { /// Creates a new `MethodFuture` from a function ABI declaration and an /// inner future. - fn new(function: Function, inner: F) -> MethodFuture { + fn new(function: Function, inner: F) -> Self { MethodFuture { function, inner } } } @@ -184,7 +179,7 @@ pub struct ViewMethodBuilder { impl ViewMethodBuilder { /// Create a new `ViewMethodBuilder` by demoting a `MethodBuilder`. - pub fn from_method(method: MethodBuilder) -> ViewMethodBuilder { + pub fn from_method(method: MethodBuilder) -> Self { ViewMethodBuilder { m: method, block: None, @@ -192,41 +187,41 @@ impl ViewMethodBuilder { } /// Apply method defaults to this builder. - pub fn with_defaults(mut self, defaults: &MethodDefaults) -> ViewMethodBuilder { + pub fn with_defaults(mut self, defaults: &MethodDefaults) -> Self { self.m = self.m.with_defaults(defaults); self } /// Specify the account the transaction is being sent from. - pub fn from(mut self, value: Address) -> ViewMethodBuilder { + pub fn from(mut self, value: Address) -> Self { self.m = self.m.from(Account::Local(value, None)); self } /// Secify amount of gas to use, if not specified then a gas estimate will /// be used. - pub fn gas(mut self, value: U256) -> ViewMethodBuilder { + pub fn gas(mut self, value: U256) -> Self { self.m = self.m.gas(value); self } /// Specify the gas price to use, if not specified then the estimated gas /// price will be used. - pub fn gas_price(mut self, value: U256) -> ViewMethodBuilder { + pub fn gas_price(mut self, value: U256) -> Self { self.m = self.m.gas_price(value); self } /// Specify what how much ETH to transfer with the transaction, if not /// specified then no ETH will be sent. - pub fn value(mut self, value: U256) -> ViewMethodBuilder { + pub fn value(mut self, value: U256) -> Self { self.m = self.m.value(value); self } /// Specify the nonce for the transation, if not specified will use the /// current transaction count for the signing account. - pub fn block(mut self, value: BlockNumber) -> ViewMethodBuilder { + pub fn block(mut self, value: BlockNumber) -> Self { self.block = Some(value); self } @@ -249,7 +244,7 @@ pub struct CallFuture { impl CallFuture { /// Construct a new `CallFuture` from a `ViewMethodBuilder`. - fn from_builder(builder: ViewMethodBuilder) -> CallFuture { + fn from_builder(builder: ViewMethodBuilder) -> Self { CallFuture { function: builder.m.function, call: builder diff --git a/src/errors.rs b/src/errors.rs index aac08f35..33909a11 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -47,7 +47,7 @@ pub enum DeployError { } impl From for DeployError { - fn from(err: AbiErrorKind) -> DeployError { + fn from(err: AbiErrorKind) -> Self { DeployError::Abi(err.into()) } } @@ -90,7 +90,7 @@ pub enum ExecutionError { } impl From for ExecutionError { - fn from(err: Web3Error) -> ExecutionError { + fn from(err: Web3Error) -> Self { match err { Web3Error::Rpc(ref err) if get_error_param(err, "error") == Some("revert") => { let reason = get_error_param(err, "reason").map(|reason| reason.to_owned()); @@ -105,7 +105,7 @@ impl From for ExecutionError { } impl From for ExecutionError { - fn from(err: AbiError) -> ExecutionError { + fn from(err: AbiError) -> Self { ExecutionError::AbiDecode(err.into()) } } @@ -145,12 +145,12 @@ pub struct MethodError { impl MethodError { /// Create a new `MethodError` from an ABI function specification and an /// inner `ExecutionError`. - pub fn new>(function: &Function, inner: I) -> MethodError { + pub fn new>(function: &Function, inner: I) -> Self { MethodError::from_parts(function_signature(function), inner.into()) } /// Create a `MethodError` from its signature and inner `ExecutionError`. - pub fn from_parts(signature: String, inner: ExecutionError) -> MethodError { + pub fn from_parts(signature: String, inner: ExecutionError) -> Self { MethodError { signature, inner } } } diff --git a/src/future.rs b/src/future.rs index 695f025a..a1174fc1 100644 --- a/src/future.rs +++ b/src/future.rs @@ -19,12 +19,12 @@ impl MaybeReady { } /// Create a new `MaybeReady` with an immediate value. - pub fn ready(value: F::Output) -> MaybeReady { + pub fn ready(value: F::Output) -> Self { MaybeReady(Either::Left(future::ready(value))) } /// Create a new `MaybeReady` with a deferred value. - pub fn future(fut: F) -> MaybeReady { + pub fn future(fut: F) -> Self { MaybeReady(Either::Right(fut)) } } diff --git a/src/test/transport.rs b/src/test/transport.rs index e092be9f..b51b6e5d 100644 --- a/src/test/transport.rs +++ b/src/test/transport.rs @@ -43,7 +43,7 @@ impl Transport for TestTransport { impl TestTransport { /// Create a new test transport instance. - pub fn new() -> TestTransport { + pub fn new() -> Self { Default::default() } diff --git a/src/transaction.rs b/src/transaction.rs index eb1b5b11..4e7d9640 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -185,7 +185,7 @@ pub struct TransactionBuilder { impl TransactionBuilder { /// Creates a new builder for a transaction. - pub fn new(web3: Web3) -> TransactionBuilder { + pub fn new(web3: Web3) -> Self { TransactionBuilder { web3, from: None, @@ -201,63 +201,63 @@ impl TransactionBuilder { /// Specify the signing method to use for the transaction, if not specified /// the the transaction will be locally signed with the default user. - pub fn from(mut self, value: Account) -> TransactionBuilder { + pub fn from(mut self, value: Account) -> Self { self.from = Some(value); self } /// Specify the recepient of the transaction, if not specified the /// transaction will be sent to the 0 address (for deploying contracts). - pub fn to(mut self, value: Address) -> TransactionBuilder { + pub fn to(mut self, value: Address) -> Self { self.to = Some(value); self } /// Secify amount of gas to use, if not specified then a gas estimate will /// be used. - pub fn gas(mut self, value: U256) -> TransactionBuilder { + pub fn gas(mut self, value: U256) -> Self { self.gas = Some(value); self } /// Specify the gas price to use, if not specified then the estimated gas /// price will be used. - pub fn gas_price(mut self, value: U256) -> TransactionBuilder { + pub fn gas_price(mut self, value: U256) -> Self { self.gas_price = Some(value); self } /// Specify what how much ETH to transfer with the transaction, if not /// specified then no ETH will be sent. - pub fn value(mut self, value: U256) -> TransactionBuilder { + pub fn value(mut self, value: U256) -> Self { self.value = Some(value); self } /// Specify the data to use for the transaction, if not specified, then empty /// data will be used. - pub fn data(mut self, value: Bytes) -> TransactionBuilder { + pub fn data(mut self, value: Bytes) -> Self { self.data = Some(value); self } /// Specify the nonce for the transation, if not specified will use the /// current transaction count for the signing account. - pub fn nonce(mut self, value: U256) -> TransactionBuilder { + pub fn nonce(mut self, value: U256) -> Self { self.nonce = Some(value); self } /// Specify the resolve condition, if not specified will default to waiting /// for the transaction to be mined (but not confirmed by any extra blocks). - pub fn resolve(mut self, value: ResolveCondition) -> TransactionBuilder { + pub fn resolve(mut self, value: ResolveCondition) -> Self { self.resolve = Some(value); self } /// Specify the number of confirmations to use for the confirmation options. /// This is a utility method for specifying the resolve condition. - pub fn confirmations(mut self, value: usize) -> TransactionBuilder { + pub fn confirmations(mut self, value: usize) -> Self { self.resolve = match self.resolve { Some(ResolveCondition::Confirmed(params)) => { Some(ResolveCondition::Confirmed(ConfirmParams { @@ -295,7 +295,7 @@ pub struct EstimateGasFuture(CompatCallFuture); impl EstimateGasFuture { /// Create a instance from a `TransactionBuilder`. - pub fn from_builder(builder: TransactionBuilder) -> EstimateGasFuture { + pub fn from_builder(builder: TransactionBuilder) -> Self { let eth = builder.web3.eth(); let from = builder.from.map(|account| account.address()); @@ -312,7 +312,7 @@ impl EstimateGasFuture { EstimateGasFuture::from_request(eth, request) } - fn from_request(eth: Eth, request: CallRequest) -> EstimateGasFuture { + fn from_request(eth: Eth, request: CallRequest) -> Self { // NOTE(nlordell): work around issue tomusdrw/rust-web3#290; while this // bas been fixed in master, it has not been released yet EstimateGasFuture( @@ -576,7 +576,7 @@ enum SendState { impl SendFuture { /// Creates a new future from a `TransactionBuilder` - pub fn from_builder(mut builder: TransactionBuilder) -> SendFuture { + pub fn from_builder(mut builder: TransactionBuilder) -> Self { let web3 = builder.web3.clone().into(); let resolve = Some(builder.resolve.take().unwrap_or_default()); let state = SendState::Building(BuildFuture::from_builder(builder)); diff --git a/src/transport.rs b/src/transport.rs index fbdce17b..1ea98ae2 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -59,7 +59,7 @@ pub struct DynTransport { impl DynTransport { /// Wrap a `Transport` in a `DynTransport` - pub fn new(inner: T) -> DynTransport + pub fn new(inner: T) -> Self where F: Future + Send + 'static, T: Transport + 'static,