-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move `impl<T> Value for Option<T>` into its own file.
- Loading branch information
1 parent
70e6863
commit 584b245
Showing
2 changed files
with
45 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! Impl Value for various container types, if the inner type implements Value. | ||
use crate::{MemoryError, Primitive, Value}; | ||
const NONE: &str = "None"; | ||
const SOME: &str = "Some"; | ||
|
||
impl<T> Value for Option<T> | ||
where | ||
T: Value, | ||
{ | ||
fn into_parts(self) -> Vec<Primitive> { | ||
match self { | ||
Some(v) => { | ||
let mut parts = Vec::new(); | ||
parts.push(SOME.to_owned().into()); | ||
parts.extend(v.into_parts()); | ||
parts | ||
} | ||
None => vec![NONE.to_owned().into()], | ||
} | ||
} | ||
|
||
fn from_parts<I>(values: &mut I) -> Result<Self, MemoryError> | ||
where | ||
I: Iterator<Item = Option<Primitive>>, | ||
{ | ||
let variant: String = values | ||
.next() | ||
.flatten() | ||
.ok_or(MemoryError::MemoryWrongSize)? | ||
.try_into()?; | ||
match variant.as_str() { | ||
NONE => Ok(None), | ||
SOME => { | ||
let val = T::from_parts(values)?; | ||
Ok(Some(val)) | ||
} | ||
other => Err(MemoryError::InvalidEnumVariant { | ||
expected_type: "option".to_owned(), | ||
actual: other.to_owned(), | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters