diff --git a/lib/modules-lib/bc-db-reader/src/indexes/certs.rs b/lib/modules-lib/bc-db-reader/src/indexes/certs.rs index db2861db8caabcf02695887f1489910181abc781..aa94ed03abf283a36edd3a4ce6dc8ea2f4aace5f 100644 --- a/lib/modules-lib/bc-db-reader/src/indexes/certs.rs +++ b/lib/modules-lib/bc-db-reader/src/indexes/certs.rs @@ -46,15 +46,9 @@ pub fn find_expire_certs<DB: DbReadable, R: DbReader>( Ok(all_expire_certs) } +#[inline] fn cert_from_u64(cert: u64) -> (WotId, WotId) { - let mut source = [0u8; 4]; - let mut target = [0u8; 4]; - let cert_bytes = cert.to_be_bytes(); - source.copy_from_slice(&cert_bytes[..4]); - target.copy_from_slice(&cert_bytes[4..]); + let (source, target) = durs_common_tools::fns::_u64::to_2_u32(cert); - ( - WotId(u32::from_be_bytes(source) as usize), - WotId(u32::from_be_bytes(target) as usize), - ) + (WotId(source as usize), WotId(target as usize)) } diff --git a/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs b/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs index fcc9c3ee16732da26e8f0c43b105d0a66a7aac20..ab7c3411fdb96d76cc8aabc671987ed113d7534b 100644 --- a/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs +++ b/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs @@ -124,17 +124,9 @@ pub fn expire_certs( Ok(()) } +#[inline] fn cert_to_u64(source: WotId, target: WotId) -> u64 { - let source_bytes = (source.0 as u32).to_be_bytes(); - let target_bytes = (target.0 as u32).to_be_bytes(); - let mut cert_u64_bytes = [0u8; 8]; - cert_u64_bytes[..4].copy_from_slice(&source_bytes[..4]); - cert_u64_bytes[4..8].copy_from_slice(&target_bytes[..4]); - /*for i in 0..4 { - cert_u64_bytes[i] = source_bytes[i]; - cert_u64_bytes[i + 4] = target_bytes[i]; - }*/ - u64::from_be_bytes(cert_u64_bytes) + durs_common_tools::fns::_u64::from_2_u32(source.0 as u32, target.0 as u32) } #[cfg(test)] diff --git a/lib/tools/common-tools/src/fns/_u64.rs b/lib/tools/common-tools/src/fns/_u64.rs new file mode 100644 index 0000000000000000000000000000000000000000..fb934bb4d198e040eb5d10e5834f27718e9d7f2b --- /dev/null +++ b/lib/tools/common-tools/src/fns/_u64.rs @@ -0,0 +1,37 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Common rust functions for extend u64 capabilities. + +/// Convert an u64 to two u32 +pub fn from_2_u32(a: u32, b: u32) -> u64 { + let a_bytes = a.to_be_bytes(); + let b_bytes = b.to_be_bytes(); + let mut u64_bytes = [0u8; 8]; + u64_bytes[..4].copy_from_slice(&a_bytes[..4]); + u64_bytes[4..8].copy_from_slice(&b_bytes[..4]); + u64::from_be_bytes(u64_bytes) +} + +/// Create an u64 from two u32 +pub fn to_2_u32(u64_: u64) -> (u32, u32) { + let mut a = [0u8; 4]; + let mut b = [0u8; 4]; + let u64_bytes = u64_.to_be_bytes(); + a.copy_from_slice(&u64_bytes[..4]); + b.copy_from_slice(&u64_bytes[4..]); + + (u32::from_be_bytes(a), u32::from_be_bytes(b)) +} diff --git a/lib/tools/common-tools/src/fns/mod.rs b/lib/tools/common-tools/src/fns/mod.rs index 0fac316676184bb9364a88a5245ca155e67dcc7a..572bed996ff9f4978e54ba8dca38254e068e9130 100644 --- a/lib/tools/common-tools/src/fns/mod.rs +++ b/lib/tools/common-tools/src/fns/mod.rs @@ -15,6 +15,7 @@ //! Common rust functions for DURS project. +pub mod _u64; pub mod arrays; pub mod bin_file; pub mod str_escape;