Skip to content

Commit 7dc2534

Browse files
gilescopebkchr
andauthored
Less slices (paritytech#9176)
* Less slices Co-authored-by: Bastian Köcher <[email protected]>
1 parent e6cd6b1 commit 7dc2534

File tree

11 files changed

+41
-46
lines changed

11 files changed

+41
-46
lines changed

client/executor/common/src/sandbox.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ fn trap(msg: &'static str) -> Trap {
187187
TrapKind::Host(Box::new(Error::Other(msg.into()))).into()
188188
}
189189

190-
fn deserialize_result(serialized_result: &[u8]) -> std::result::Result<Option<RuntimeValue>, Trap> {
190+
fn deserialize_result(mut serialized_result: &[u8]) -> std::result::Result<Option<RuntimeValue>, Trap> {
191191
use self::sandbox_primitives::HostError;
192192
use sp_wasm_interface::ReturnValue;
193-
let result_val = std::result::Result::<ReturnValue, HostError>::decode(&mut &serialized_result[..])
193+
let result_val = std::result::Result::<ReturnValue, HostError>::decode(&mut serialized_result)
194194
.map_err(|_| trap("Decoding Result<ReturnValue, HostError> failed!"))?;
195195

196196
match result_val {
@@ -379,10 +379,10 @@ pub enum InstantiationError {
379379
}
380380

381381
fn decode_environment_definition(
382-
raw_env_def: &[u8],
382+
mut raw_env_def: &[u8],
383383
memories: &[Option<MemoryRef>],
384384
) -> std::result::Result<(Imports, GuestToSupervisorFunctionMapping), InstantiationError> {
385-
let env_def = sandbox_primitives::EnvironmentDefinition::decode(&mut &raw_env_def[..])
385+
let env_def = sandbox_primitives::EnvironmentDefinition::decode(&mut raw_env_def)
386386
.map_err(|_| InstantiationError::EnvironmentDefinitionCorrupted)?;
387387

388388
let mut func_map = HashMap::new();

client/executor/src/wasm_runtime.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl RuntimeCache {
177177

178178
/// Prepares a WASM module instance and executes given function for it.
179179
///
180-
/// This uses internal cache to find avaiable instance or create a new one.
180+
/// This uses internal cache to find available instance or create a new one.
181181
/// # Parameters
182182
///
183183
/// `code` - Provides external code or tells the executor to fetch it from storage.
@@ -196,7 +196,7 @@ impl RuntimeCache {
196196
///
197197
/// `f` - Function to execute.
198198
///
199-
/// # Returns result of `f` wrapped in an additonal result.
199+
/// # Returns result of `f` wrapped in an additional result.
200200
/// In case of failure one of two errors can be returned:
201201
///
202202
/// `Err::InvalidCode` is returned for runtime code issues.
@@ -337,7 +337,7 @@ pub fn create_wasm_runtime_with_code(
337337
}
338338
}
339339

340-
fn decode_version(version: &[u8]) -> Result<RuntimeVersion, WasmError> {
340+
fn decode_version(mut version: &[u8]) -> Result<RuntimeVersion, WasmError> {
341341
let v: RuntimeVersion = sp_api::OldRuntimeVersion::decode(&mut &version[..])
342342
.map_err(|_|
343343
WasmError::Instantiation(
@@ -347,7 +347,7 @@ fn decode_version(version: &[u8]) -> Result<RuntimeVersion, WasmError> {
347347

348348
let core_api_id = sp_core::hashing::blake2_64(b"Core");
349349
if v.has_api_with(&core_api_id, |v| v >= 3) {
350-
sp_api::RuntimeVersion::decode(&mut &version[..])
350+
sp_api::RuntimeVersion::decode(&mut version)
351351
.map_err(|_|
352352
WasmError::Instantiation("failed to decode \"Core_version\" result".into())
353353
)
@@ -367,9 +367,7 @@ fn decode_runtime_apis(apis: &[u8]) -> Result<Vec<([u8; 8], u32)>, WasmError> {
367367
<[u8; RUNTIME_API_INFO_SIZE]>::try_from(chunk)
368368
.map(sp_api::deserialize_runtime_api_info)
369369
.map_err(|_| {
370-
WasmError::Other(format!(
371-
"a clipped runtime api info declaration"
372-
))
370+
WasmError::Other("a clipped runtime api info declaration".to_owned())
373371
})
374372
})
375373
.collect::<Result<Vec<_>, WasmError>>()
@@ -383,15 +381,15 @@ fn decode_runtime_apis(apis: &[u8]) -> Result<Vec<([u8; 8], u32)>, WasmError> {
383381
pub fn read_embedded_version(
384382
blob: &RuntimeBlob,
385383
) -> Result<Option<RuntimeVersion>, WasmError> {
386-
if let Some(version_section) = blob.custom_section_contents("runtime_version") {
384+
if let Some(mut version_section) = blob.custom_section_contents("runtime_version") {
387385
// We do not use `decode_version` here because the runtime_version section is not supposed
388386
// to ever contain a legacy version. Apart from that `decode_version` relies on presence
389387
// of a special API in the `apis` field to treat the input as a non-legacy version. However
390388
// the structure found in the `runtime_version` always contain an empty `apis` field. Therefore
391-
// the version read will be mistakingly treated as an legacy one.
392-
let mut decoded_version = sp_api::RuntimeVersion::decode(&mut &version_section[..])
389+
// the version read will be mistakenly treated as an legacy one.
390+
let mut decoded_version = sp_api::RuntimeVersion::decode(&mut version_section)
393391
.map_err(|_|
394-
WasmError::Instantiation("failed to decode verison section".into())
392+
WasmError::Instantiation("failed to decode version section".into())
395393
)?;
396394

397395
// Don't stop on this and check if there is a special section that encodes all runtime APIs.

client/executor/wasmi/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
185185
&mut self,
186186
instance_id: u32,
187187
export_name: &str,
188-
args: &[u8],
188+
mut args: &[u8],
189189
return_val: Pointer<u8>,
190190
return_val_len: WordSize,
191191
state: u32,
192192
) -> WResult<u32> {
193193
trace!(target: "sp-sandbox", "invoke, instance_idx={}", instance_id);
194194

195195
// Deserialize arguments and convert them into wasmi types.
196-
let args = Vec::<sp_wasm_interface::Value>::decode(&mut &args[..])
196+
let args = Vec::<sp_wasm_interface::Value>::decode(&mut args)
197197
.map_err(|_| "Can't decode serialized arguments for the invocation")?
198198
.into_iter()
199199
.map(Into::into)

primitives/io/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929

3030
use sp_std::vec::Vec;
3131

32-
#[cfg(feature = "std")]
33-
use sp_std::ops::Deref;
34-
3532
#[cfg(feature = "std")]
3633
use tracing;
3734

@@ -990,7 +987,7 @@ pub trait Offchain {
990987
.local_storage_compare_and_set(
991988
kind,
992989
key,
993-
old_value.as_ref().map(|v| v.deref()),
990+
old_value.as_deref(),
994991
new_value,
995992
)
996993
}

primitives/runtime/src/generic/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8>
4444
let size = ::sp_std::mem::size_of::<T>();
4545
let reserve = match size {
4646
0..=0b00111111 => 1,
47-
0..=0b00111111_11111111 => 2,
47+
0b01000000..=0b00111111_11111111 => 2,
4848
_ => 4,
4949
};
5050
let mut v = Vec::with_capacity(reserve + size);

primitives/state-machine/src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub trait Backend<H: Hasher>: sp_std::fmt::Debug {
207207
}
208208
}
209209
let (root, parent_txs) = self.storage_root(delta
210-
.map(|(k, v)| (&k[..], v.as_ref().map(|v| &v[..])))
210+
.map(|(k, v)| (k, v.as_ref().map(|v| &v[..])))
211211
.chain(
212212
child_roots
213213
.iter()

primitives/state-machine/src/changes_trie/build.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,22 +279,22 @@ fn prepare_digest_input<'a, H, Number>(
279279
trie_root,
280280
);
281281

282-
trie_storage.for_key_values_with_prefix(&child_prefix, |key, value|
283-
if let Ok(InputKey::ChildIndex::<Number>(trie_key)) = Decode::decode(&mut &key[..]) {
284-
if let Ok(value) = <Vec<u8>>::decode(&mut &value[..]) {
282+
trie_storage.for_key_values_with_prefix(&child_prefix, |mut key, mut value|
283+
if let Ok(InputKey::ChildIndex::<Number>(trie_key)) = Decode::decode(&mut key) {
284+
if let Ok(value) = <Vec<u8>>::decode(&mut value) {
285285
let mut trie_root = <H as Hasher>::Out::default();
286286
trie_root.as_mut().copy_from_slice(&value[..]);
287287
children_roots.insert(trie_key.storage_key, trie_root);
288288
}
289289
});
290290

291-
trie_storage.for_keys_with_prefix(&extrinsic_prefix, |key|
292-
if let Ok(InputKey::ExtrinsicIndex::<Number>(trie_key)) = Decode::decode(&mut &key[..]) {
291+
trie_storage.for_keys_with_prefix(&extrinsic_prefix, |mut key|
292+
if let Ok(InputKey::ExtrinsicIndex::<Number>(trie_key)) = Decode::decode(&mut key) {
293293
insert_to_map(&mut map, trie_key.key);
294294
});
295295

296-
trie_storage.for_keys_with_prefix(&digest_prefix, |key|
297-
if let Ok(InputKey::DigestIndex::<Number>(trie_key)) = Decode::decode(&mut &key[..]) {
296+
trie_storage.for_keys_with_prefix(&digest_prefix, |mut key|
297+
if let Ok(InputKey::DigestIndex::<Number>(trie_key)) = Decode::decode(&mut key) {
298298
insert_to_map(&mut map, trie_key.key);
299299
});
300300
}
@@ -310,13 +310,13 @@ fn prepare_digest_input<'a, H, Number>(
310310
crate::changes_trie::TrieBackendStorageAdapter(storage),
311311
trie_root,
312312
);
313-
trie_storage.for_keys_with_prefix(&extrinsic_prefix, |key|
314-
if let Ok(InputKey::ExtrinsicIndex::<Number>(trie_key)) = Decode::decode(&mut &key[..]) {
313+
trie_storage.for_keys_with_prefix(&extrinsic_prefix, |mut key|
314+
if let Ok(InputKey::ExtrinsicIndex::<Number>(trie_key)) = Decode::decode(&mut key) {
315315
insert_to_map(&mut map, trie_key.key);
316316
});
317317

318-
trie_storage.for_keys_with_prefix(&digest_prefix, |key|
319-
if let Ok(InputKey::DigestIndex::<Number>(trie_key)) = Decode::decode(&mut &key[..]) {
318+
trie_storage.for_keys_with_prefix(&digest_prefix, |mut key|
319+
if let Ok(InputKey::DigestIndex::<Number>(trie_key)) = Decode::decode(&mut key) {
320320
insert_to_map(&mut map, trie_key.key);
321321
});
322322
}

primitives/state-machine/src/changes_trie/prune.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ pub fn prune<H: Hasher, Number: BlockNumber, F: FnMut(H::Out)>(
6666
);
6767
let child_prefix = ChildIndex::key_neutral_prefix(block.clone());
6868
let mut children_roots = Vec::new();
69-
trie_storage.for_key_values_with_prefix(&child_prefix, |key, value| {
70-
if let Ok(InputKey::ChildIndex::<Number>(_trie_key)) = Decode::decode(&mut &key[..]) {
71-
if let Ok(value) = <Vec<u8>>::decode(&mut &value[..]) {
69+
trie_storage.for_key_values_with_prefix(&child_prefix, |mut key, mut value| {
70+
if let Ok(InputKey::ChildIndex::<Number>(_trie_key)) = Decode::decode(&mut key) {
71+
if let Ok(value) = <Vec<u8>>::decode(&mut value) {
7272
let mut trie_root = <H as Hasher>::Out::default();
7373
trie_root.as_mut().copy_from_slice(&value[..]);
7474
children_roots.push(trie_root);

primitives/state-machine/src/ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ where
637637
}
638638

639639
#[cfg(feature = "std")]
640-
fn storage_changes_root(&mut self, parent_hash: &[u8]) -> Result<Option<Vec<u8>>, ()> {
640+
fn storage_changes_root(&mut self, mut parent_hash: &[u8]) -> Result<Option<Vec<u8>>, ()> {
641641
let _guard = guard();
642642
if let Some(ref root) = self.storage_transaction_cache.changes_trie_transaction_storage_root {
643643
trace!(
@@ -653,7 +653,7 @@ where
653653
let root = self.overlay.changes_trie_root(
654654
self.backend,
655655
self.changes_trie_state.as_ref(),
656-
Decode::decode(&mut &parent_hash[..]).map_err(|e|
656+
Decode::decode(&mut parent_hash).map_err(|e|
657657
trace!(
658658
target: "state",
659659
"Failed to decode changes root parent hash: {}",

primitives/trie/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub fn read_trie_value<L: TrieConfiguration, DB: hash_db::HashDBRef<L::Hash, tri
211211
root: &TrieHash<L>,
212212
key: &[u8]
213213
) -> Result<Option<Vec<u8>>, Box<TrieError<L>>> {
214-
Ok(TrieDB::<L>::new(&*db, root)?.get(key).map(|x| x.map(|val| val.to_vec()))?)
214+
TrieDB::<L>::new(&*db, root)?.get(key).map(|x| x.map(|val| val.to_vec()))
215215
}
216216

217217
/// Read a value from the trie with given Query.
@@ -225,7 +225,7 @@ pub fn read_trie_value_with<
225225
key: &[u8],
226226
query: Q
227227
) -> Result<Option<Vec<u8>>, Box<TrieError<L>>> {
228-
Ok(TrieDB::<L>::new(&*db, root)?.get_with(key, query).map(|x| x.map(|val| val.to_vec()))?)
228+
TrieDB::<L>::new(&*db, root)?.get_with(key, query).map(|x| x.map(|val| val.to_vec()))
229229
}
230230

231231
/// Determine the empty trie root.
@@ -317,7 +317,7 @@ pub fn read_child_trie_value<L: TrieConfiguration, DB>(
317317
root.as_mut().copy_from_slice(root_slice);
318318

319319
let db = KeySpacedDB::new(&*db, keyspace);
320-
Ok(TrieDB::<L>::new(&db, &root)?.get(key).map(|x| x.map(|val| val.to_vec()))?)
320+
TrieDB::<L>::new(&db, &root)?.get(key).map(|x| x.map(|val| val.to_vec()))
321321
}
322322

323323
/// Read a value from the child trie with given query.
@@ -336,7 +336,7 @@ pub fn read_child_trie_value_with<L: TrieConfiguration, Q: Query<L::Hash, Item=D
336336
root.as_mut().copy_from_slice(root_slice);
337337

338338
let db = KeySpacedDB::new(&*db, keyspace);
339-
Ok(TrieDB::<L>::new(&db, &root)?.get_with(key, query).map(|x| x.map(|val| val.to_vec()))?)
339+
TrieDB::<L>::new(&db, &root)?.get_with(key, query).map(|x| x.map(|val| val.to_vec()))
340340
}
341341

342342
/// `HashDB` implementation that append a encoded prefix (unique id bytes) in addition to the

0 commit comments

Comments
 (0)