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 <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2019-07-02 17:11:09 +02:00
parent 0b7fb42a6c
commit 77684f473d

View File

@ -63,6 +63,25 @@ enum VirtioDeviceType {
TYPE_VSOCK = 19,
TYPE_FS = 26,
TYPE_PMEM = 27,
TYPE_UNKNOWN = 0xFF,
}
impl From<u32> 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