catbuffer_rust/difficulty_dto.rs
1/*
2 * // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
3 * // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
4 * // All rights reserved.
5 * //
6 * // This file is part of Catapult.
7 * //
8 * // Catapult is free software: you can redistribute it and/or modify
9 * // it under the terms of the GNU Lesser General Public License as published by
10 * // the Free Software Foundation, either version 3 of the License, or
11 * // (at your option) any later version.
12 * //
13 * // Catapult is distributed in the hope that it will be useful,
14 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * // GNU Lesser General Public License for more details.
17 * //
18 * // You should have received a copy of the GNU Lesser General Public License
19 * // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22use super::generator_utils::*;
23
24/// Difficulty.
25#[derive(Debug, Clone, Copy)]
26pub struct DifficultyDto(pub u64);
27
28impl DifficultyDto {
29 pub const LENGTH: usize = std::mem::size_of::<Self>();
30
31 /// Gets the size of the type.
32 ///
33 /// # Returns
34 /// A usize.
35 pub fn get_size(&self) -> usize {
36 Self::LENGTH
37 }
38
39 /// Gets Difficulty.
40 ///
41 /// # Returns
42 /// A Difficulty.
43 pub fn get_difficulty(&self) -> u64 {
44 self.0
45 }
46
47 /// Serializes an type to bytes.
48 ///
49 /// # Returns
50 /// A Serialized bytes.
51 pub fn serializer(&self) -> Vec<u8> {
52 self.0.to_le_bytes().to_vec()
53 }
54
55 /// Creates an `DifficultyDto` from a slice.
56 ///
57 /// # Returns
58 /// A `DifficultyDto`.
59 pub fn from_binary(src: &[u8]) -> Self {
60 // assert_eq!(src.len(), Self::LENGTH);
61 let buf = fixed_bytes::<{ Self::LENGTH }>(src);
62 Self(u64::from_le_bytes(buf))
63 }
64}