rust-vmm / vhost

Apache License 2.0
130 stars 69 forks source link

Proper enum <-> value conversion #202

Closed germag closed 10 months ago

germag commented 10 months ago

Summary of the PR

NOOP and MAX_CMD are not present in the vhost-user protocol, these are use only to verify that the value is correct before transmuting it to an enum variant.

This has two problems, firstly it exposes of non-standard values in a public interface, this internal implementation detail could be confusing, and secondly it assumes that the values are consecutive with no "holes" between them.

This results in having to define requests that are not actually supported, and error-prone, which can generate UB when transmuting.

Instead, it is better to implement TryFrom<u32> for enums that need to convert between variants and u32. This allows to remove the unsafe transmute.