Add From ByteSize for u64
Converting from a ByteSize to a u64 is ByteSize's main purpose. From
impls are the standard way to convert to/from types. This commit also
removes a function get_mem_size that did exactly the same thing but in a
less standard way.
// See the License for the specific language governing permissions and
// limitations under the License.
+use std::convert::From;
use std::str::FromStr;
use serde::de::Visitor;
@@ -23,12 +24,6 @@ pub struct ByteSize {
unit: MemoryUnitSize,
}
-impl ByteSize {
- pub fn get_mem_size(&self) -> u64 {
- (self.size * self.unit.byte_count() as f32).trunc() as u64
- }
-}
-
#[derive(Clone, Debug)]
enum MemoryUnitSize {
Kilobyte,
@@ -58,6 +53,12 @@ impl MemoryUnitSize {
}
}
+impl From<ByteSize> for u64 {
+ fn from(bytes: ByteSize) -> Self {
+ (bytes.size * bytes.unit.byte_count() as f32).trunc() as u64
+ }
+}
+
impl<'de> Deserialize<'de> for ByteSize {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
