Skip to content
Snippets Groups Projects
Commit 22599cb4 authored by Éloïs's avatar Éloïs
Browse files

[ref] blockchain: move u64<->(u32,u32) conversion into common-tools

parent d3ee15fa
No related branches found
No related tags found
1 merge request!216[ref] blockchain: move u64<->(u32,u32) conversion into common-tools
...@@ -46,15 +46,9 @@ pub fn find_expire_certs<DB: DbReadable, R: DbReader>( ...@@ -46,15 +46,9 @@ pub fn find_expire_certs<DB: DbReadable, R: DbReader>(
Ok(all_expire_certs) Ok(all_expire_certs)
} }
#[inline]
fn cert_from_u64(cert: u64) -> (WotId, WotId) { fn cert_from_u64(cert: u64) -> (WotId, WotId) {
let mut source = [0u8; 4]; let (source, target) = durs_common_tools::fns::_u64::to_2_u32(cert);
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..]);
( (WotId(source as usize), WotId(target as usize))
WotId(u32::from_be_bytes(source) as usize),
WotId(u32::from_be_bytes(target) as usize),
)
} }
...@@ -124,17 +124,9 @@ pub fn expire_certs( ...@@ -124,17 +124,9 @@ pub fn expire_certs(
Ok(()) Ok(())
} }
#[inline]
fn cert_to_u64(source: WotId, target: WotId) -> u64 { fn cert_to_u64(source: WotId, target: WotId) -> u64 {
let source_bytes = (source.0 as u32).to_be_bytes(); durs_common_tools::fns::_u64::from_2_u32(source.0 as u32, target.0 as u32)
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)
} }
#[cfg(test)] #[cfg(test)]
......
// 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))
}
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
//! Common rust functions for DURS project. //! Common rust functions for DURS project.
pub mod _u64;
pub mod arrays; pub mod arrays;
pub mod bin_file; pub mod bin_file;
pub mod str_escape; pub mod str_escape;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment