memu/
lib.rs

1#![allow(clippy::tabs_in_doc_comments)]
2#![deny(clippy::missing_const_for_fn)]
3#![deny(missing_docs)]
4
5/*!
6# memu
7The memu crate provides rust implementations of digital storage (or memory) units.
8The maximum amount of storage is `18446.744073709553 Petabyte`, conversion between units always happends with a factor of `1024`.
9
10The create also provides conversions between units and information about the units such as, unit suffixes and scale factors. As well as optional serde compatibility.
11
12Licensed under MIT.
13
14# Example
15In the following example we first crate a `KiloByte` with 913 Kilobytes. We then print it as Gigabytes.
16
17```Rust
18use memu::units::MegaByte;
19
20let kilo_byte = MegaByte::from(913);
21
22println!("{}", kilo_byte.as_giga_byte().as_string_with_unit());
23
24```
25
26In the next example we first fetch some system info using the `sysinfo` crate and then print the memory usage for every process.
27We use the `KiloByte::new()` method here, since `process.memory()` returns bytes and the new method constructs the unit from an amount of bytes.
28
29
30```Rust
31use memu::units::KiloByte;
32use sysinfo::{ProcessExt, System, SystemExt};
33
34fn main() {
35	let mut sys = System::new_all();
36	sys.refresh_all();
37
38	for (pid, process) in sys.processes() {
39		let memory = KiloByte::new(process.memory());
40		println!("{}: {}", pid, memory.as_string_with_unit())
41	}
42}
43```
44
45Now we use normal addition with the units to sum all of the processes memory.
46
47```Rust
48use memu::units::Byte;
49use sysinfo::{ProcessExt, System, SystemExt};
50
51fn main() {
52	let mut sys = System::new_all();
53	sys.refresh_all();
54
55	let mut total = Byte::default();
56
57	for (pid, process) in sys.processes() {
58		total += process.memory();
59	}
60
61	println!("Total: {}", total.as_gigabyte().as_string_with_unit())
62
63	let average = total / sys.processes().len();
64
65	println!("Average: {}", average.as_gigabyte().as_string_with_unit())
66}
67```
68
69# Features
70The crate contains the following features:
71-  serde, serialize and deserialize data with the serde crate.
72-  macro, use macros to create the storage structs. Enabled by default.
73-  units, include text units such as "KB" and "TB". Enabled by default.
74*/
75
76use constants::{GIGABYTE, KILOBYTE, MEGABYTE, TERABYTE};
77use units::{Byte, GigaByte, KiloByte, MegaByte, PetaByte, TeraByte};
78
79/// The core memory units.
80pub mod units;
81
82/// Memory constants for conversion between units.
83pub mod constants;
84
85/// Macros for easy creation of memory units.
86#[doc(hidden)]
87#[cfg(feature = "macro")]
88pub mod macros;
89
90#[doc(hidden)]
91mod bytes_impl;
92
93/// Trait for converting between memory units.
94pub trait MemoryUnit {
95	/// Converts the unit into `bits`.
96	fn as_bits(&self) -> u128;
97	/// Converts the unit into [`Byte`]s.
98	fn as_byte(&self) -> Byte;
99	/// Converts the unit into [`KiloByte`]s.
100	fn as_kilo_byte(&self) -> KiloByte;
101	/// Converts the unit into [`MegaByte`]s.
102	fn as_mega_byte(&self) -> MegaByte;
103	/// Converts the unit into [`GigaByte`]s.
104	fn as_giga_byte(&self) -> GigaByte;
105	/// Converts the unit into [`TeraByte`]s.
106	fn as_tera_byte(&self) -> TeraByte;
107	/// Converts the unit into [`PetaByte`]s.
108	fn as_peta_byte(&self) -> PetaByte;
109}
110
111/// This functions finds the first memory unit without a
112/// leading 0 infront of the comma.
113pub fn best_fit(bytes: u64) -> Box<dyn MemoryUnit> {
114	// There most likley is a faster way to do this using bitshifts by 10
115	// and then comparing something..?
116	if bytes < KILOBYTE {
117		Box::new(Byte::new(bytes))
118	} else if bytes < MEGABYTE {
119		Box::new(KiloByte::new(bytes))
120	} else if bytes < GIGABYTE {
121		Box::new(MegaByte::new(bytes))
122	} else if bytes < TERABYTE {
123		Box::new(GigaByte::new(bytes))
124	} else {
125		Box::new(PetaByte::new(bytes))
126	}
127}