File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -11,3 +11,4 @@ documentation = "https://docs.rs/enumflags2"
11
11
12
12
[dependencies ]
13
13
enumflags2_derive = { version = " 0.6.0" , path = " ../enumflags_derive" }
14
+ serde = { version = " ^1.0.0" , default-features = false , optional = true }
Original file line number Diff line number Diff line change @@ -321,3 +321,36 @@ where
321
321
flags
322
322
}
323
323
}
324
+
325
+ #[ cfg( feature = "serde" ) ]
326
+ mod impl_serde {
327
+ extern crate serde;
328
+ use self :: serde:: { Serialize , Deserialize } ;
329
+ use self :: serde:: de:: { Error , Unexpected } ;
330
+ use super :: { BitFlags , _internal:: RawBitFlags } ;
331
+
332
+ impl < ' a , T > Deserialize < ' a > for BitFlags < T >
333
+ where
334
+ T : RawBitFlags ,
335
+ T :: Type : Deserialize < ' a > + Into < u64 > ,
336
+ {
337
+ fn deserialize < D : serde:: Deserializer < ' a > > ( d : D ) -> Result < Self , D :: Error > {
338
+ let val = T :: Type :: deserialize ( d) ?;
339
+ Self :: from_bits ( val)
340
+ . ok_or_else ( || D :: Error :: invalid_value (
341
+ Unexpected :: Unsigned ( val. into ( ) ) ,
342
+ & "valid bit representation"
343
+ ) )
344
+ }
345
+ }
346
+
347
+ impl < T > Serialize for BitFlags < T >
348
+ where
349
+ T : RawBitFlags ,
350
+ T :: Type : Serialize ,
351
+ {
352
+ fn serialize < S : serde:: Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
353
+ T :: Type :: serialize ( & self . val , s)
354
+ }
355
+ }
356
+ }
You can’t perform that action at this time.
0 commit comments