git.delta.rocks / unique-network / refs/commits / 0a5eeb039c86

difftreelog

minor: change calculating size for simple types.

Trubnikov Sergey2022-08-19parent: #3114b4f.patch.diff
in: master

1 file changed

modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
330pub trait AbiRead<T> {330pub trait AbiRead<T> {
331 /// Read item from current position, advanding decoder331 /// Read item from current position, advanding decoder
332 fn abi_read(&mut self) -> Result<T>;332 fn abi_read(&mut self) -> Result<T>;
333 fn size() -> usize;
333}334}
334335
335macro_rules! impl_abi_readable {336macro_rules! impl_abi_readable {
339 self.$method()340 self.$method()
340 }341 }
342
343 fn size() -> usize {
344 ABI_ALIGNMENT
345 }
341 }346 }
342 };347 };
343}348}
365impl<R: sealed::CanBePlacedInVec> AbiRead<Vec<R>> for AbiReader<'_>370impl<R: sealed::CanBePlacedInVec> AbiRead<Vec<R>> for AbiReader<'_>
366where371where
367 Self: AbiRead<R>,372 Self: AbiRead<R>,
368{373{
369 fn abi_read(&mut self) -> Result<Vec<R>> {374 fn abi_read(&mut self) -> Result<Vec<R>> {
370 let mut sub = self.subresult(None)?;375 let mut sub = self.subresult(None)?;
371 let size = sub.uint32()? as usize;376 let size = sub.uint32()? as usize;
376 }381 }
377 Ok(out)382 Ok(out)
378 }383 }
379}
380384
381fn aligned_size(size: usize) -> usize {385 fn size() -> usize {
382 let need_align = (size % ABI_ALIGNMENT) != 0;
383 let aligned_parts = size / ABI_ALIGNMENT;
384 (aligned_parts * ABI_ALIGNMENT) + if need_align { ABI_ALIGNMENT } else { 0 }386 ABI_ALIGNMENT
385}387 }
386388}
387#[test]
388fn test_aligned_size() {
389 assert_eq!(aligned_size(20), ABI_ALIGNMENT);
390 assert_eq!(aligned_size(32), ABI_ALIGNMENT);
391 assert_eq!(aligned_size(52), 2 * ABI_ALIGNMENT);
392 assert_eq!(aligned_size(64), 2 * ABI_ALIGNMENT);
393}
394389
395macro_rules! impl_tuples {390macro_rules! impl_tuples {
396 ($($ident:ident)+) => {391 ($($ident:ident)+) => {
403 ($($ident,)+): SolidityTypeName,398 ($($ident,)+): SolidityTypeName,
404 {399 {
405 fn abi_read(&mut self) -> Result<($($ident,)+)> {400 fn abi_read(&mut self) -> Result<($($ident,)+)> {
406 let size = if <($($ident,)+)>::is_simple() { Some(0 $(+aligned_size(sp_std::mem::size_of::<$ident>()))+) } else { None };401 let size = if <($($ident,)+)>::is_simple() { Some(<Self as AbiRead<($($ident,)+)>>::size()) } else { None };
407 let mut subresult = self.subresult(size)?;402 let mut subresult = self.subresult(size)?;
408 Ok((403 Ok((
409 $(<Self as AbiRead<$ident>>::abi_read(&mut subresult)?,)+404 $(<Self as AbiRead<$ident>>::abi_read(&mut subresult)?,)+
410 ))405 ))
411 }406 }
407
408 fn size() -> usize {
409 0 $(+ {let _ : $ident; ABI_ALIGNMENT})+
410 }
412 }411 }
413 #[allow(non_snake_case)]412 #[allow(non_snake_case)]
414 impl<$($ident),+> AbiWrite for &($($ident,)+)413 impl<$($ident),+> AbiWrite for &($($ident,)+)