Skip to content

Commit

Permalink
refactor: replace a From<&Vec<Foo>> by FromIterator<Foo>
Browse files Browse the repository at this point in the history
This is more versatile, makes the construction more visible, and can be used with collect.
  • Loading branch information
huitseeker committed Apr 30, 2022
1 parent 93c424c commit 26fce70
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 11 additions & 5 deletions sui_types/src/move_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ impl MovePackage {
}
}

impl From<&Vec<CompiledModule>> for MovePackage {
fn from(compiled_modules: &Vec<CompiledModule>) -> Self {
let id = ObjectID::from(*compiled_modules[0].self_id().address());
impl FromIterator<CompiledModule> for MovePackage {
fn from_iter<T: IntoIterator<Item = CompiledModule>>(iter: T) -> Self {
let mut iter = iter.into_iter().peekable();
let id = ObjectID::from(
*iter
.peek()
.expect("Tried to build a Move package from an empty iterator of Compiled modules")
.self_id()
.address(),
);

Self::new(
id,
&compiled_modules
.iter()
&iter
.map(|module| {
let mut bytes = Vec::new();
module.serialize(&mut bytes).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion sui_types/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,13 @@ impl Object {
}
}

// Note: this will panic if `modules` is empty
pub fn new_package(
modules: Vec<CompiledModule>,
previous_transaction: TransactionDigest,
) -> Self {
Object {
data: Data::Package(MovePackage::from(&modules)),
data: Data::Package(MovePackage::from_iter(modules)),
owner: Owner::Immutable,
previous_transaction,
storage_rebate: 0,
Expand Down

0 comments on commit 26fce70

Please sign in to comment.