Distinguish between IO and pure functions

This commit is contained in:
Edsko de Vries 2023-03-20 12:36:23 +01:00
parent 6c48f2357f
commit b9e13b0dbd

View file

@ -13,6 +13,10 @@ module Foreign.Rust.Marshall.Variable (
, withBorshVarBuffer , withBorshVarBuffer
, withBorshMaxBuffer , withBorshMaxBuffer
, withBorshFailure , withBorshFailure
-- ** Pure variants
, withPureBorshVarBuffer
, withPureBorshMaxBuffer
, withPureBorshFailure
) where ) where
import Codec.Borsh import Codec.Borsh
@ -66,7 +70,7 @@ withBorshVarBuffer :: forall a.
, StaticBorshSize a ~ 'HasVariableSize , StaticBorshSize a ~ 'HasVariableSize
, Typeable a , Typeable a
) )
=> (Buffer a -> IO ()) -> a => (Buffer a -> IO ()) -> IO a
withBorshVarBuffer = withBorshBufferOfInitSize 1024 withBorshVarBuffer = withBorshBufferOfInitSize 1024
withBorshMaxBuffer :: forall a. withBorshMaxBuffer :: forall a.
@ -75,7 +79,7 @@ withBorshMaxBuffer :: forall a.
, BorshMaxSize a , BorshMaxSize a
, Typeable a , Typeable a
) )
=> (Buffer a -> IO ()) -> a => (Buffer a -> IO ()) -> IO a
withBorshMaxBuffer = withBorshMaxBuffer =
withBorshBufferOfInitSize initBufSize withBorshBufferOfInitSize initBufSize
where where
@ -89,8 +93,39 @@ withBorshFailure :: forall a.
, Typeable a , Typeable a
, HasCallStack , HasCallStack
) )
=> (Buffer (Either Text a) -> IO ()) -> IO (Either Failure a)
withBorshFailure = fmap (first mkFailure) . withBorshVarBuffer
{-------------------------------------------------------------------------------
Pure variants
-------------------------------------------------------------------------------}
withPureBorshVarBuffer :: forall a.
( FromBorsh a
, StaticBorshSize a ~ 'HasVariableSize
, Typeable a
)
=> (Buffer a -> IO ()) -> a
withPureBorshVarBuffer = unsafePerformIO . withBorshVarBuffer
withPureBorshMaxBuffer :: forall a.
( FromBorsh a
, StaticBorshSize a ~ 'HasVariableSize
, BorshMaxSize a
, Typeable a
)
=> (Buffer a -> IO ()) -> a
withPureBorshMaxBuffer = unsafePerformIO . withBorshMaxBuffer
withPureBorshFailure :: forall a.
( FromBorsh a
, StaticBorshSize a ~ 'HasVariableSize
, Typeable a
, HasCallStack
)
=> (Buffer (Either Text a) -> IO ()) -> Either Failure a => (Buffer (Either Text a) -> IO ()) -> Either Failure a
withBorshFailure = first mkFailure . withBorshVarBuffer withPureBorshFailure = unsafePerformIO . withBorshFailure
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
Internal auxiliary Internal auxiliary
@ -102,8 +137,8 @@ withBorshBufferOfInitSize :: forall a.
, StaticBorshSize a ~ 'HasVariableSize , StaticBorshSize a ~ 'HasVariableSize
, Typeable a , Typeable a
) )
=> CULong -> (Buffer a -> IO ()) -> a => CULong -> (Buffer a -> IO ()) -> IO a
withBorshBufferOfInitSize initBufSize f = unsafePerformIO $ do withBorshBufferOfInitSize initBufSize f = do
mFirstAttempt <- allocaBytes (culongToInt initBufSize) $ \buf -> do mFirstAttempt <- allocaBytes (culongToInt initBufSize) $ \buf -> do
(bigEnough, reqSz) <- callWithSize buf initBufSize (bigEnough, reqSz) <- callWithSize buf initBufSize
if bigEnough then if bigEnough then