BorshMaxSize should work similar to _fixed_ size

(instead of similiar to variable size, as it was prior to this commit.)
This commit is contained in:
Edsko de Vries 2023-03-22 08:03:44 +01:00
parent f30df93fb5
commit 7e18d48951
2 changed files with 19 additions and 31 deletions

View file

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

View file

@ -11,11 +11,9 @@ module Foreign.Rust.Marshall.Variable (
, Buffer -- opaque
, getVarBuffer
, withBorshVarBuffer
, withBorshMaxBuffer
, withBorshFailure
-- ** Pure variants
, withPureBorshVarBuffer
, withPureBorshMaxBuffer
, withPureBorshFailure
) where
@ -73,19 +71,6 @@ withBorshVarBuffer :: forall a.
=> (Buffer a -> IO ()) -> IO a
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
withBorshFailure :: forall a.
( FromBorsh a
@ -108,15 +93,6 @@ withPureBorshVarBuffer :: forall 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
@ -126,7 +102,6 @@ withPureBorshFailure :: forall a.
=> (Buffer (Either Text a) -> IO ()) -> Either Failure a
withPureBorshFailure = unsafePerformIO . withBorshFailure
{-------------------------------------------------------------------------------
Internal auxiliary
-------------------------------------------------------------------------------}