From 77684f473da95e406f84943a471b92d22f7ad5d3 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Tue, 2 Jul 2019 17:11:09 +0200 Subject: [PATCH] vm-virtio: Implement the u32 to VirtioDeviceType conversion The From trait allows us to compare and convert an integer with and into a virtio device type. Signed-off-by: Samuel Ortiz --- vm-virtio/src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/vm-virtio/src/lib.rs b/vm-virtio/src/lib.rs index d9d31afda..9acb7d796 100755 --- a/vm-virtio/src/lib.rs +++ b/vm-virtio/src/lib.rs @@ -63,6 +63,25 @@ enum VirtioDeviceType { TYPE_VSOCK = 19, TYPE_FS = 26, TYPE_PMEM = 27, + TYPE_UNKNOWN = 0xFF, +} + +impl From for VirtioDeviceType { + fn from(t: u32) -> Self { + match t { + 1 => VirtioDeviceType::TYPE_NET, + 2 => VirtioDeviceType::TYPE_BLOCK, + 4 => VirtioDeviceType::TYPE_RNG, + 5 => VirtioDeviceType::TYPE_BALLOON, + 9 => VirtioDeviceType::TYPE_9P, + 16 => VirtioDeviceType::TYPE_GPU, + 18 => VirtioDeviceType::TYPE_INPUT, + 19 => VirtioDeviceType::TYPE_VSOCK, + 26 => VirtioDeviceType::TYPE_FS, + 27 => VirtioDeviceType::TYPE_PMEM, + _ => VirtioDeviceType::TYPE_UNKNOWN, + } + } } // In order to use the `{}` marker, the trait `fmt::Display` must be implemented