@@ -49,11 +49,7 @@ impl<'a> Value<'a> {
4949
5050 /// Returns true if this is a NULL value
5151 pub fn is_null ( & self ) -> bool {
52- if let ValueInner :: NULL = self . 0 {
53- true
54- } else {
55- false
56- }
52+ matches ! ( self . 0 , ValueInner :: NULL )
5753 }
5854
5955 pub ( crate ) fn parse_from (
@@ -169,12 +165,12 @@ impl<'a> ValueInner<'a> {
169165 }
170166}
171167
172- // NOTE: these should all be TryInto
168+ // NOTE: these should all be TryFrom
173169macro_rules! impl_into {
174170 ( $t: ty, $( $variant: path) ,* ) => {
175- impl <' a> Into <$t> for Value <' a> {
176- fn into ( self ) -> $t {
177- match self . 0 {
171+ impl <' a> From < Value <' a>> for $t {
172+ fn from ( val : Value < ' a> ) -> Self {
173+ match val . 0 {
178174 $( $variant( v) => v as $t) ,* ,
179175 v => panic!( concat!( "invalid type conversion from {:?} to " , stringify!( $t) ) , v)
180176 }
@@ -195,35 +191,35 @@ impl_into!(f32, ValueInner::Double);
195191impl_into ! ( f64 , ValueInner :: Double ) ;
196192impl_into ! ( & ' a [ u8 ] , ValueInner :: Bytes ) ;
197193
198- impl < ' a > Into < & ' a str > for Value < ' a > {
199- fn into ( self ) -> & ' a str {
200- if let ValueInner :: Bytes ( v) = self . 0 {
194+ impl < ' a > From < Value < ' a > > for & ' a str {
195+ fn from ( val : Value < ' a > ) -> Self {
196+ if let ValueInner :: Bytes ( v) = val . 0 {
201197 :: std:: str:: from_utf8 ( v) . unwrap ( )
202198 } else {
203- panic ! ( "invalid type conversion from {:?} to string" , self )
199+ panic ! ( "invalid type conversion from {:?} to string" , val )
204200 }
205201 }
206202}
207203
208204use chrono:: { NaiveDate , NaiveDateTime } ;
209- impl < ' a > Into < NaiveDate > for Value < ' a > {
210- fn into ( self ) -> NaiveDate {
211- if let ValueInner :: Date ( mut v) = self . 0 {
205+ impl < ' a > From < Value < ' a > > for NaiveDate {
206+ fn from ( val : Value < ' a > ) -> Self {
207+ if let ValueInner :: Date ( mut v) = val . 0 {
212208 assert_eq ! ( v. len( ) , 4 ) ;
213209 NaiveDate :: from_ymd (
214210 i32:: from ( v. read_u16 :: < LittleEndian > ( ) . unwrap ( ) ) ,
215211 u32:: from ( v. read_u8 ( ) . unwrap ( ) ) ,
216212 u32:: from ( v. read_u8 ( ) . unwrap ( ) ) ,
217213 )
218214 } else {
219- panic ! ( "invalid type conversion from {:?} to date" , self )
215+ panic ! ( "invalid type conversion from {:?} to date" , val )
220216 }
221217 }
222218}
223219
224- impl < ' a > Into < NaiveDateTime > for Value < ' a > {
225- fn into ( self ) -> NaiveDateTime {
226- if let ValueInner :: Datetime ( mut v) = self . 0 {
220+ impl < ' a > From < Value < ' a > > for NaiveDateTime {
221+ fn from ( val : Value < ' a > ) -> Self {
222+ if let ValueInner :: Datetime ( mut v) = val . 0 {
227223 assert ! ( v. len( ) == 7 || v. len( ) == 11 ) ;
228224 let d = NaiveDate :: from_ymd (
229225 i32:: from ( v. read_u16 :: < LittleEndian > ( ) . unwrap ( ) ) ,
@@ -242,15 +238,15 @@ impl<'a> Into<NaiveDateTime> for Value<'a> {
242238 d. and_hms ( h, m, s)
243239 }
244240 } else {
245- panic ! ( "invalid type conversion from {:?} to datetime" , self )
241+ panic ! ( "invalid type conversion from {:?} to datetime" , val )
246242 }
247243 }
248244}
249245
250246use std:: time:: Duration ;
251- impl < ' a > Into < Duration > for Value < ' a > {
252- fn into ( self ) -> Duration {
253- if let ValueInner :: Time ( mut v) = self . 0 {
247+ impl < ' a > From < Value < ' a > > for Duration {
248+ fn from ( val : Value < ' a > ) -> Self {
249+ if let ValueInner :: Time ( mut v) = val . 0 {
254250 assert ! ( v. is_empty( ) || v. len( ) == 8 || v. len( ) == 12 ) ;
255251
256252 if v. is_empty ( ) {
@@ -277,7 +273,7 @@ impl<'a> Into<Duration> for Value<'a> {
277273 micros * 1_000 ,
278274 )
279275 } else {
280- panic ! ( "invalid type conversion from {:?} to datetime" , self )
276+ panic ! ( "invalid type conversion from {:?} to datetime" , val )
281277 }
282278 }
283279}
0 commit comments