Skip to content

Commit 6aaf882

Browse files
committed
Add more conversion methods for Optional
1 parent ba526a5 commit 6aaf882

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/boot_info.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,35 @@ pub enum Optional<T> {
210210
None,
211211
}
212212

213+
impl<T> Optional<T> {
214+
/// Converts the `Optional` to an [`Option`].
215+
pub fn into_option(self) -> Option<T> {
216+
self.into()
217+
}
218+
219+
/// Converts from `&Optional<T>` to `Option<&T>`.
220+
///
221+
/// For convenience, this method directly performs the conversion to the standard
222+
/// [`Option`] type.
223+
pub const fn as_ref(&self) -> Option<&T> {
224+
match self {
225+
Self::Some(x) => Some(x),
226+
Self::None => None,
227+
}
228+
}
229+
230+
/// Converts from `&mut Optional<T>` to `Option<&mut T>`.
231+
///
232+
/// For convenience, this method directly performs the conversion to the standard
233+
/// [`Option`] type.
234+
pub fn as_mut(&mut self) -> Option<&mut T> {
235+
match self {
236+
Self::Some(x) => Some(x),
237+
Self::None => None,
238+
}
239+
}
240+
}
241+
213242
impl<T> From<Option<T>> for Optional<T> {
214243
fn from(v: Option<T>) -> Self {
215244
match v {

0 commit comments

Comments
 (0)