Skip to content

Commit eca340a

Browse files
Seulgi Kimsgkim126
authored andcommitted
Implement TryFrom instead of ugly From for Result
1 parent 03d6df9 commit eca340a

File tree

6 files changed

+40
-29
lines changed

6 files changed

+40
-29
lines changed

rpc/src/v1/impls/account.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::convert::TryInto;
1718
use std::sync::Arc;
1819
use std::time::Duration;
1920

@@ -98,7 +99,7 @@ where
9899
static ref LOCK: Mutex<()> = Mutex::new(());
99100
}
100101
let _guard = LOCK.lock();
101-
let (tx, seq): (IncompleteTransaction, Option<u64>) = ::std::result::Result::from(tx)?;
102+
let (tx, seq): (IncompleteTransaction, Option<u64>) = tx.try_into()?;
102103

103104
let (hash, seq) = self
104105
.miner

rpc/src/v1/impls/chain.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::convert::{TryFrom, TryInto};
1718
use std::sync::Arc;
1819

1920
use ccore::{
@@ -273,7 +274,7 @@ where
273274

274275
fn execute_transaction(&self, tx: UnsignedTransaction, sender: PlatformAddress) -> Result<Option<String>> {
275276
let sender_address = sender.try_address().map_err(errors::core)?;
276-
let action = ::std::result::Result::from(tx.action).map_err(errors::conversion)?;
277+
let action = Action::try_from(tx.action).map_err(errors::conversion)?;
277278
if let Some(transaction) = action.asset_transaction() {
278279
let result = self.client.execute_transaction(&transaction, sender_address);
279280
match result {
@@ -291,7 +292,7 @@ where
291292
params: Vec<Vec<BytesArray>>,
292293
indices: Vec<usize>,
293294
) -> Result<Vec<String>> {
294-
let action = ::std::result::Result::from(tx.action).map_err(errors::conversion)?;
295+
let action = tx.action.try_into().map_err(errors::conversion)?;
295296
if let Action::TransferAsset {
296297
inputs,
297298
..

rpc/src/v1/types/action.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use std::iter::FromIterator;
17+
use std::convert::TryFrom;
1818

1919
use cjson::uint::Uint;
2020
use ckey::{NetworkId, PlatformAddress, Public, Signature};
2121
use ctypes::transaction::{Action as ActionType, AssetMintOutput as AssetMintOutputType};
2222
use ctypes::ShardId;
2323
use primitives::{Bytes, H160, H256};
24-
use rustc_serialize::hex::{FromHex, FromHexError, ToHex};
24+
use rustc_serialize::hex::{FromHex, ToHex};
2525

2626
use super::super::errors::ConversionError;
2727
use super::{AssetMintOutput, AssetTransferInput, AssetTransferOutput, OrderOnTransfer};
@@ -489,9 +489,9 @@ impl ActionWithTracker {
489489
}
490490
}
491491

492-
// FIXME: Use TryFrom.
493-
impl From<Action> for Result<ActionType, ConversionError> {
494-
fn from(from: Action) -> Self {
492+
impl TryFrom<Action> for ActionType {
493+
type Error = ConversionError;
494+
fn try_from(from: Action) -> Result<Self, Self::Error> {
495495
Ok(match from {
496496
Action::MintAsset {
497497
network_id,
@@ -511,7 +511,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
511511
Some(registrar) => Some(registrar.try_into_address()?),
512512
None => None,
513513
};
514-
let output_content = Result::<AssetMintOutputType, FromHexError>::from(*output)?;
514+
let output_content = AssetMintOutputType::try_from(*output)?;
515515
ActionType::MintAsset {
516516
network_id,
517517
shard_id,
@@ -534,13 +534,13 @@ impl From<Action> for Result<ActionType, ConversionError> {
534534
approvals,
535535
expiration,
536536
} => {
537-
let iter_outputs = outputs.into_iter().map(From::from);
538-
let orders = orders.into_iter().map(From::from).collect::<Result<_, _>>()?;
537+
let outputs = outputs.into_iter().map(TryFrom::try_from).collect::<Result<_, _>>()?;
538+
let orders = orders.into_iter().map(TryFrom::try_from).collect::<Result<_, _>>()?;
539539
ActionType::TransferAsset {
540540
network_id,
541541
burns: burns.into_iter().map(From::from).collect(),
542542
inputs: inputs.into_iter().map(From::from).collect(),
543-
outputs: Result::from_iter(iter_outputs)?,
543+
outputs,
544544
orders,
545545
metadata,
546546
approvals,
@@ -587,7 +587,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
587587
output,
588588
approvals,
589589
} => {
590-
let output_content = Result::<AssetMintOutputType, FromHexError>::from(*output)?;
590+
let output_content = AssetMintOutputType::try_from(*output)?;
591591
ActionType::IncreaseAssetSupply {
592592
network_id,
593593
shard_id,
@@ -617,7 +617,7 @@ impl From<Action> for Result<ActionType, ConversionError> {
617617
Some(registrar) => Some(registrar.try_into_address()?),
618618
None => None,
619619
};
620-
let output_content = Result::<AssetMintOutputType, FromHexError>::from(*output)?;
620+
let output_content = AssetMintOutputType::try_from(*output)?;
621621
ActionType::ComposeAsset {
622622
network_id,
623623
shard_id,
@@ -637,11 +637,11 @@ impl From<Action> for Result<ActionType, ConversionError> {
637637

638638
approvals,
639639
} => {
640-
let iter_outputs = outputs.into_iter().map(From::from);
640+
let outputs = outputs.into_iter().map(TryFrom::try_from).collect::<Result<_, _>>()?;
641641
ActionType::DecomposeAsset {
642642
network_id,
643643
input: (*input).into(),
644-
outputs: Result::from_iter(iter_outputs)?,
644+
outputs,
645645
approvals,
646646
}
647647
}

rpc/src/v1/types/asset_output.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
extern crate rustc_serialize;
1717

18+
use std::convert::TryFrom;
1819
use std::iter::FromIterator;
1920
use std::ops::Deref;
2021

@@ -46,8 +47,9 @@ impl From<AssetTransferOutputType> for AssetTransferOutput {
4647
}
4748
}
4849

49-
impl From<AssetTransferOutput> for Result<AssetTransferOutputType, FromHexError> {
50-
fn from(from: AssetTransferOutput) -> Self {
50+
impl TryFrom<AssetTransferOutput> for AssetTransferOutputType {
51+
type Error = FromHexError;
52+
fn try_from(from: AssetTransferOutput) -> Result<Self, Self::Error> {
5153
Ok(AssetTransferOutputType {
5254
lock_script_hash: from.lock_script_hash,
5355
parameters: Result::from_iter(from.parameters.iter().map(Deref::deref).map(FromHex::from_hex))?,
@@ -76,8 +78,9 @@ impl From<AssetMintOutputType> for AssetMintOutput {
7678
}
7779
}
7880

79-
impl From<AssetMintOutput> for Result<AssetMintOutputType, FromHexError> {
80-
fn from(from: AssetMintOutput) -> Self {
81+
impl TryFrom<AssetMintOutput> for AssetMintOutputType {
82+
type Error = FromHexError;
83+
fn try_from(from: AssetMintOutput) -> Result<Self, Self::Error> {
8184
Ok(AssetMintOutputType {
8285
lock_script_hash: from.lock_script_hash,
8386
parameters: Result::from_iter(from.parameters.iter().map(Deref::deref).map(FromHex::from_hex))?,

rpc/src/v1/types/order.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::convert::{TryFrom, TryInto};
18+
1719
use cjson::uint::Uint;
1820
use ctypes::transaction::{Order as OrderType, OrderOnTransfer as OrderOnTransferType};
1921
use ctypes::ShardId;
@@ -64,8 +66,9 @@ impl From<OrderType> for Order {
6466
}
6567
}
6668

67-
impl From<Order> for Result<OrderType, FromHexError> {
68-
fn from(from: Order) -> Self {
69+
impl TryFrom<Order> for OrderType {
70+
type Error = FromHexError;
71+
fn try_from(from: Order) -> Result<Self, Self::Error> {
6972
let parameters_from =
7073
from.parameters_from.into_iter().map(|param| param.from_hex()).collect::<Result<_, _>>()?;
7174
let parameters_fee = from.parameters_fee.into_iter().map(|param| param.from_hex()).collect::<Result<_, _>>()?;
@@ -109,10 +112,11 @@ impl From<OrderOnTransferType> for OrderOnTransfer {
109112
}
110113
}
111114

112-
impl From<OrderOnTransfer> for Result<OrderOnTransferType, FromHexError> {
113-
fn from(from: OrderOnTransfer) -> Self {
115+
impl TryFrom<OrderOnTransfer> for OrderOnTransferType {
116+
type Error = FromHexError;
117+
fn try_from(from: OrderOnTransfer) -> Result<Self, Self::Error> {
114118
Ok(OrderOnTransferType {
115-
order: Result::from(from.order)?,
119+
order: from.order.try_into()?,
116120
spent_quantity: from.spent_quantity.into(),
117121
input_indices: from.input_indices,
118122
output_indices: from.output_indices,

rpc/src/v1/types/unsigned_transaction.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use std::convert::{TryFrom, TryInto};
18+
1719
use cjson::uint::Uint;
1820
use ckey::NetworkId;
1921
use ctypes::transaction::IncompleteTransaction;
@@ -31,14 +33,14 @@ pub struct UnsignedTransaction {
3133
pub action: Action,
3234
}
3335

34-
// FIXME: Use TryFrom.
35-
impl From<UnsignedTransaction> for Result<(IncompleteTransaction, Option<u64>), Error> {
36-
fn from(tx: UnsignedTransaction) -> Self {
36+
impl TryFrom<UnsignedTransaction> for (IncompleteTransaction, Option<u64>) {
37+
type Error = Error;
38+
fn try_from(tx: UnsignedTransaction) -> Result<Self, Self::Error> {
3739
Ok((
3840
IncompleteTransaction {
3941
fee: tx.fee.into(),
4042
network_id: tx.network_id,
41-
action: Result::from(tx.action).map_err(errors::conversion)?,
43+
action: tx.action.try_into().map_err(errors::conversion)?,
4244
},
4345
tx.seq,
4446
))

0 commit comments

Comments
 (0)