@@ -208,17 +208,21 @@ deriving via CDouble instance FromPy Double
208208instance ToPy Float where basicToPy = basicToPy . float2Double
209209instance FromPy Float where basicFromPy = fmap double2Float . basicFromPy
210210
211+ -- | @since 0.2
211212instance ToPy (Complex Float ) where
212213 basicToPy (x:+ y) = basicToPy $ float2Double x :+ float2Double y
214+ -- | @since 0.2
213215instance FromPy (Complex Float ) where
214216 basicFromPy xy_py = do
215217 x :+ y <- basicFromPy xy_py
216218 return $ double2Float x :+ double2Float y
217219
220+ -- | @since 0.2
218221instance ToPy (Complex Double ) where
219222 basicToPy (x:+ y) = Py [CU. exp | PyObject* { PyComplex_FromDoubles($(double x'), $(double y')) } |]
220223 where x' = CDouble x
221224 y' = CDouble y
225+ -- | @since 0.2
222226instance FromPy (Complex Double ) where
223227 basicFromPy xy_py = do
224228 CDouble x <- Py [CU. exp | double { PyComplex_RealAsDouble($(PyObject *xy_py)) } |]
@@ -412,6 +416,24 @@ instance (FromPy a, FromPy b, FromPy c, FromPy d) => FromPy (a,b,c,d) where
412416 d <- basicFromPy p_d
413417 pure (a,b,c,d)
414418
419+
420+ -- | @Nothing@ is encoded as @None@. @Just a@ same as @a@.
421+ --
422+ -- @since 0.2
423+ instance (ToPy a ) => ToPy (Maybe a ) where
424+ basicToPy Nothing = Py [CU. exp | PyObject* { Py_None } |]
425+ basicToPy (Just a) = basicToPy a
426+
427+ -- | @None@ is decoded as @Nothing@ rest is attempted to be decoded as @a@
428+ --
429+ -- @since 0.2
430+ instance (FromPy a ) => FromPy (Maybe a ) where
431+ basicFromPy p =
432+ Py [CU. exp | bool { Py_None == $(PyObject *p) } |] >>= \ case
433+ 0 -> Just <$> basicFromPy p
434+ _ -> pure Nothing
435+
436+
415437instance (ToPy a ) => ToPy [a ] where
416438 basicToPy = basicListToPy
417439
@@ -566,7 +588,9 @@ vectorToPy vec = runProgram $ do
566588 n_c = fromIntegral n :: CLLong
567589
568590
569- -- | @since NEXT_VERSION@. Converted to @bytes@
591+ -- | Converted to @bytes@
592+ --
593+ -- @since 0.2
570594instance ToPy BS. ByteString where
571595 basicToPy bs = pyIO $ BS. unsafeUseAsCStringLen bs $ \ (ptr,len) -> do
572596 let c_len = fromIntegral len :: CLLong
@@ -575,7 +599,9 @@ instance ToPy BS.ByteString where
575599 NULL -> unsafeRunPy mustThrowPyError
576600 _ -> return py
577601
578- -- | @since NEXT_VERSION@. Accepts @bytes@ and @bytearray@
602+ -- | Accepts @bytes@ and @bytearray@
603+ --
604+ -- @since 0.2
579605instance FromPy BS. ByteString where
580606 basicFromPy py = pyIO $ do
581607 [CU. exp | int { PyBytes_Check($(PyObject* py)) } |] >>= \ case
@@ -595,16 +621,22 @@ instance FromPy BS.ByteString where
595621 copyBytes hs_buf py_buf sz
596622 BS. unsafePackMallocCStringLen (hs_buf, sz)
597623
598- -- | @since NEXT_VERSION@. Converted to @bytes@
624+ -- | Converted to @bytes@
625+ --
626+ -- @since 0.2
599627instance ToPy BL. ByteString where
600628 basicToPy = basicToPy . BL. toStrict
601629
602- -- | @since NEXT_VERSION@. Accepts @bytes@ and @bytearray@
630+ -- | Accepts @bytes@ and @bytearray@
631+ --
632+ -- @since 0.2
603633instance FromPy BL. ByteString where
604634 basicFromPy = fmap BL. fromStrict . basicFromPy
605635
606636
607- -- | @since NEXT_VERSION@. Accepts @bytes@ and @bytearray@
637+ -- | Accepts @bytes@ and @bytearray@
638+ --
639+ -- @since 0.2
608640instance FromPy SBS. ShortByteString where
609641 basicFromPy py = pyIO $ do
610642 [CU. exp | int { PyBytes_Check($(PyObject* py)) } |] >>= \ case
@@ -623,7 +655,9 @@ instance FromPy SBS.ShortByteString where
623655 bs <- BS. unsafePackCStringLen (buf, sz)
624656 evaluate $ SBS. toShort bs
625657
626- -- | @since NEXT_VERSION@. Converted to @bytes@
658+ -- | Converted to @bytes@
659+ --
660+ -- @since 0.2
627661instance ToPy SBS. ShortByteString where
628662 basicToPy bs = pyIO $ SBS. useAsCStringLen bs $ \ (ptr,len) -> do
629663 let c_len = fromIntegral len :: CLLong
@@ -633,7 +667,7 @@ instance ToPy SBS.ShortByteString where
633667 _ -> return py
634668
635669
636- -- | @since NEXT_VERSION @.
670+ -- | @since 0.2 @.
637671instance ToPy T. Text where
638672 -- NOTE: Is there ore efficient way to access
639673 basicToPy str = pyIO $ BS. unsafeUseAsCStringLen bs $ \ (ptr,len) -> do
@@ -645,11 +679,11 @@ instance ToPy T.Text where
645679 where
646680 bs = T. encodeUtf8 str
647681
648- -- | @since NEXT_VERSION @.
682+ -- | @since 0.2 @.
649683instance ToPy TL. Text where
650684 basicToPy = basicToPy . TL. toStrict
651685
652- -- | @since NEXT_VERSION @.
686+ -- | @since 0.2 @.
653687instance FromPy T. Text where
654688 basicFromPy py = pyIO $ do
655689 [CU. exp | int { PyUnicode_Check($(PyObject* py)) } |] >>= \ case
@@ -660,7 +694,7 @@ instance FromPy T.Text where
660694 return $! T. decodeUtf8Lenient bs
661695 _ -> throwM BadPyType
662696
663- -- | @since NEXT_VERSION @.
697+ -- | @since 0.2 @.
664698instance FromPy TL. Text where
665699 basicFromPy = fmap TL. fromStrict . basicFromPy
666700
0 commit comments