Skip to content

Commit e50d1b4

Browse files
authored
Merge pull request #15 from rust-osdev/recommendations
feat(features): add `recommendations` and `recommendations_satisfied`
2 parents 24180ef + a7517a1 commit e50d1b4

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

src/features.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ where
1313
///
1414
/// If `self` is a single feature and multiple features are returned, `self` requires only one of them.
1515
///
16+
/// # Driver Requirements
17+
///
18+
/// The driver MUST NOT accept a feature which requires another feature which was not accepted.
19+
///
20+
/// # Device Requirements
21+
///
22+
/// The device MUST NOT offer a feature which requires another feature which was not offered.
23+
///
1624
/// # Examples
1725
///
1826
/// ```
@@ -52,6 +60,50 @@ where
5260
.filter(|requirements| !requirements.is_empty())
5361
.all(|requirements| self.intersects(requirements))
5462
}
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+
}
55107
}
56108

57109
endian_bitflags! {
@@ -498,6 +550,21 @@ pub mod net {
498550

499551
requirements
500552
}
553+
554+
fn recommendations(&self) -> Self {
555+
let mut recommendations = Self::empty();
556+
557+
for feature in self.iter() {
558+
let recommendation = match feature {
559+
Self::HASH_REPORT => Self::CTRL_VQ,
560+
Self::CTRL_RX_EXTRA => Self::CTRL_VQ,
561+
_ => Self::empty(),
562+
};
563+
recommendations.insert(recommendation);
564+
}
565+
566+
recommendations
567+
}
501568
}
502569
}
503570

0 commit comments

Comments
 (0)