diff --git a/src/bson.rs b/src/bson.rs index b53bfdc1..899eeaf5 100644 --- a/src/bson.rs +++ b/src/bson.rs @@ -26,6 +26,7 @@ use std::{ convert::TryFrom, fmt::{self, Debug, Display, Formatter}, hash::Hash, + num::NonZero, ops::Index, }; @@ -383,15 +384,27 @@ impl> ::std::iter::FromIterator for Bson { } } +impl From for Bson { + fn from(a: i64) -> Bson { + Bson::Int64(a) + } +} + impl From for Bson { fn from(a: i32) -> Bson { Bson::Int32(a) } } -impl From for Bson { - fn from(a: i64) -> Bson { - Bson::Int64(a) +impl From for Bson { + fn from(value: i16) -> Self { + Bson::Int32(value.into()) + } +} + +impl From for Bson { + fn from(value: i8) -> Self { + Bson::Int32(value.into()) } } @@ -405,6 +418,32 @@ impl From for Bson { } } +impl From for Bson { + fn from(value: u16) -> Self { + Bson::Int32(value.into()) + } +} + +// From for Bson would cause the From<[u8; 12]> impl and the generic From<[T; N]> impl to +// overlap. +// TODO RUST-2317: remove the overspecialized From<[u8; 12]> impl. + +// NonZero is bounded by T: ZeroablePrimitive, and ZeroablePrimitive is unstable so we can't just +// write a generic impl :( +macro_rules! from_nonzero { + ($($t:ty),+) => { + $( + impl From> for Bson { + fn from(value: NonZero<$t>) -> Self { + Bson::from(value.get()) + } + } + )+ + }; +} + +from_nonzero!(i64, i32, i16, i8, u32, u16); + impl From<[u8; 12]> for Bson { fn from(a: [u8; 12]) -> Bson { Bson::ObjectId(oid::ObjectId::from_bytes(a))