Merge pull request #3 from BeFunctional/edsko/maxsize

`BorshMaxSize` should work similar to _fixed_ size
This commit is contained in:
Edsko de Vries 2023-03-22 11:16:06 +00:00 committed by GitHub
commit dd3eed5940
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 31 deletions

View file

@ -9,7 +9,8 @@ module Foreign.Rust.Marshall.Fixed (
toBorshFixed toBorshFixed
-- * Rust to Haskell -- * Rust to Haskell
, allocFixedBuffer , allocFixedBuffer
, fromBorshFixed , allocMaxBuffer
, fromBorsh
) where ) where
import Codec.Borsh import Codec.Borsh
@ -33,7 +34,7 @@ toBorshFixed a k =
Strict.useAsCStringLen (serialiseStrict a) (k . castFromSignedLen) Strict.useAsCStringLen (serialiseStrict a) (k . castFromSignedLen)
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
Rust to Haskell Rust to Haskell: exact size known
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
allocFixedBuffer :: forall a. allocFixedBuffer :: forall a.
@ -47,9 +48,21 @@ allocFixedBuffer k =
cast :: Word32 -> Int cast :: Word32 -> Int
cast = fromIntegral cast = fromIntegral
fromBorshFixed :: allocMaxBuffer :: forall a.
(FromBorsh a, StaticBorshSize a ~ 'HasKnownSize, Typeable a) ( BorshSize a
=> Ptr CUChar -> CULong -> IO a , StaticBorshSize a ~ 'HasVariableSize
fromBorshFixed ptr len = , BorshMaxSize a
)
=> ((Ptr CUChar, CULong) -> IO a) -> IO a
allocMaxBuffer k =
let n = borshMaxSize (Proxy @a)
in allocaBytes (cast n) $ \ptr -> k (ptr, fromIntegral n)
where
cast :: Word32 -> Int
cast = fromIntegral
fromBorsh :: (FromBorsh a, Typeable a) => Ptr CUChar -> CULong -> IO a
fromBorsh ptr len =
deserialiseStrictOrPanic <$> deserialiseStrictOrPanic <$>
Strict.packCStringLen (castToSigned ptr, fromIntegral len) Strict.packCStringLen (castToSigned ptr, fromIntegral len)

View file

@ -11,11 +11,9 @@ module Foreign.Rust.Marshall.Variable (
, Buffer -- opaque , Buffer -- opaque
, getVarBuffer , getVarBuffer
, withBorshVarBuffer , withBorshVarBuffer
, withBorshMaxBuffer
, withBorshFailure , withBorshFailure
-- ** Pure variants -- ** Pure variants
, withPureBorshVarBuffer , withPureBorshVarBuffer
, withPureBorshMaxBuffer
, withPureBorshFailure , withPureBorshFailure
) where ) where
@ -73,19 +71,6 @@ withBorshVarBuffer :: forall a.
=> (Buffer a -> IO ()) -> IO a => (Buffer a -> IO ()) -> IO a
withBorshVarBuffer = withBorshBufferOfInitSize 1024 withBorshVarBuffer = withBorshBufferOfInitSize 1024
withBorshMaxBuffer :: forall a.
( FromBorsh a
, StaticBorshSize a ~ 'HasVariableSize
, BorshMaxSize a
, Typeable a
)
=> (Buffer a -> IO ()) -> IO a
withBorshMaxBuffer =
withBorshBufferOfInitSize initBufSize
where
initBufSize :: CULong
initBufSize = fromIntegral $ borshMaxSize (Proxy @a)
-- | Wrapper around 'withBorshVarBuffer' with explicit support for failures -- | Wrapper around 'withBorshVarBuffer' with explicit support for failures
withBorshFailure :: forall a. withBorshFailure :: forall a.
( FromBorsh a ( FromBorsh a
@ -108,15 +93,6 @@ withPureBorshVarBuffer :: forall a.
=> (Buffer a -> IO ()) -> a => (Buffer a -> IO ()) -> a
withPureBorshVarBuffer = unsafePerformIO . withBorshVarBuffer 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. withPureBorshFailure :: forall a.
( FromBorsh a ( FromBorsh a
, StaticBorshSize a ~ 'HasVariableSize , StaticBorshSize a ~ 'HasVariableSize
@ -126,7 +102,6 @@ withPureBorshFailure :: forall a.
=> (Buffer (Either Text a) -> IO ()) -> Either Failure a => (Buffer (Either Text a) -> IO ()) -> Either Failure a
withPureBorshFailure = unsafePerformIO . withBorshFailure withPureBorshFailure = unsafePerformIO . withBorshFailure
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
Internal auxiliary Internal auxiliary
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}