Skip to content

Commit a7517a1

Browse files
committed
feat(features): add recommendations and recommendations_satisfied
1 parent 2786202 commit a7517a1

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

src/features.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,50 @@ where
6060
.filter(|requirements| !requirements.is_empty())
6161
.all(|requirements| self.intersects(requirements))
6262
}
63+
64+
/// Returns the feature that this feature recommends.
65+
///
66+
/// If `self` is a single feature and multiple features are returned, `self` recommendns only one of them.
67+
///
68+
/// # Driver Requirements
69+
///
70+
/// The driver SHOULD NOT accept a feature which recommends another feature which was not accepted.
71+
///
72+
/// # Device Requirements
73+
///
74+
/// The device SHOULD NOT offer a feature which recommends another feature which was not offered.
75+
///
76+
/// # Examples
77+
///
78+
/// ```
79+
/// # use virtio_spec as virtio;
80+
/// use virtio::FeatureBits;
81+
///
82+
/// assert_eq!(
83+
/// virtio::net::F::HASH_REPORT.recommendations(),
84+
/// virtio::net::F::CTRL_VQ
85+
/// );
86+
/// ```
87+
fn recommendations(&self) -> Self {
88+
Self::empty()
89+
}
90+
91+
/// Returns `true` if all internal feature recommendations are satisfied.
92+
///
93+
/// # Examples
94+
///
95+
/// ```
96+
/// # use virtio_spec as virtio;
97+
/// use virtio::FeatureBits;
98+
///
99+
/// assert!((virtio::net::F::HASH_REPORT | virtio::net::F::CTRL_VQ).recommendations_satisfied());
100+
/// ```
101+
fn recommendations_satisfied(&self) -> bool {
102+
self.iter()
103+
.map(|feature| feature.recommendations())
104+
.filter(|recommendations| !recommendations.is_empty())
105+
.all(|recommendations| self.intersects(recommendations))
106+
}
63107
}
64108

65109
endian_bitflags! {
@@ -496,6 +540,21 @@ pub mod net {
496540

497541
requirements
498542
}
543+
544+
fn recommendations(&self) -> Self {
545+
let mut recommendations = Self::empty();
546+
547+
for feature in self.iter() {
548+
let recommendation = match feature {
549+
Self::HASH_REPORT => Self::CTRL_VQ,
550+
Self::CTRL_RX_EXTRA => Self::CTRL_VQ,
551+
_ => Self::empty(),
552+
};
553+
recommendations.insert(recommendation);
554+
}
555+
556+
recommendations
557+
}
499558
}
500559
}
501560

0 commit comments

Comments
 (0)