File tree Expand file tree Collapse file tree 2 files changed +11
-6
lines changed
binary/src/Cardano/Binary Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ module Cardano.Binary.FromCBOR
13
13
, enforceSize
14
14
, matchSize
15
15
, module D
16
+ , fromCBORMaybe
16
17
)
17
18
where
18
19
@@ -298,15 +299,15 @@ instance FromCBOR a => FromCBOR (NonEmpty a) where
298
299
Just xs -> Right xs
299
300
300
301
instance FromCBOR a => FromCBOR (Maybe a ) where
301
- fromCBOR = decodeMaybe fromCBOR
302
+ fromCBOR = fromCBORMaybe fromCBOR
302
303
303
- decodeMaybe :: D. Decoder s a -> D. Decoder s (Maybe a )
304
- decodeMaybe decoder = do
304
+ fromCBORMaybe :: D. Decoder s a -> D. Decoder s (Maybe a )
305
+ fromCBORMaybe fromCBORA = do
305
306
n <- D. decodeListLen
306
307
case n of
307
308
0 -> return Nothing
308
309
1 -> do
309
- ! x <- decoder
310
+ ! x <- fromCBORA
310
311
return (Just x)
311
312
_ -> cborError $ DecoderErrorUnknownTag " Maybe" (fromIntegral n)
312
313
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ module Cardano.Binary.ToCBOR
14
14
( ToCBOR (.. )
15
15
, withWordSize
16
16
, module E
17
+ , toCBORMaybe
17
18
18
19
-- * Size of expressions
19
20
, Range (.. )
@@ -584,12 +585,15 @@ instance ToCBOR a => ToCBOR (NonEmpty a) where
584
585
encodedSizeExpr size _ = size (Proxy @ [a ]) -- MN TODO make 0 count impossible
585
586
586
587
instance ToCBOR a => ToCBOR (Maybe a ) where
587
- toCBOR Nothing = E. encodeListLen 0
588
- toCBOR (Just x) = E. encodeListLen 1 <> toCBOR x
588
+ toCBOR = toCBORMaybe toCBOR
589
589
590
590
encodedSizeExpr size _ =
591
591
szCases [Case " Nothing" 1 , Case " Just" (1 + size (Proxy @ a ))]
592
592
593
+ toCBORMaybe :: (a -> Encoding ) -> Maybe a -> Encoding
594
+ toCBORMaybe encodeA = \ case
595
+ Nothing -> E. encodeListLen 0
596
+ Just x -> E. encodeListLen 1 <> encodeA x
593
597
594
598
encodeContainerSkel
595
599
:: (Word -> E. Encoding )
You can’t perform that action at this time.
0 commit comments