NonZero constructor now returns Option

This commit is contained in:
David Tolnay 2017-07-27 00:35:56 -07:00
parent ccec002bf3
commit 9f0973aff7
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -1573,14 +1573,9 @@ where
D: Deserializer<'de>,
{
let value = try!(Deserialize::deserialize(deserializer));
unsafe {
let ptr = &value as *const T as *const u8;
if slice::from_raw_parts(ptr, mem::size_of::<T>()).iter().all(|&b| b == 0) {
return Err(Error::custom("expected a non-zero value"));
}
// Waiting for a safe way to construct NonZero<T>:
// https://github.com/rust-lang/rust/issues/27730#issuecomment-269726075
Ok(NonZero::new(value))
match NonZero::new(value) {
Some(nonzero) => Ok(nonzero),
None => Err(Error::custom("expected a non-zero value")),
}
}
}