From 7e18d48951315caebe5056a3fbd3788cdc681e1c Mon Sep 17 00:00:00 2001 From: Edsko de Vries Date: Wed, 22 Mar 2023 08:03:44 +0100 Subject: [PATCH] `BorshMaxSize` should work similar to _fixed_ size (instead of similiar to variable size, as it was prior to this commit.) --- src/Foreign/Rust/Marshall/Fixed.hs | 25 +++++++++++++++++++------ src/Foreign/Rust/Marshall/Variable.hs | 25 ------------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/Foreign/Rust/Marshall/Fixed.hs b/src/Foreign/Rust/Marshall/Fixed.hs index dc7d91d..2e2fd26 100644 --- a/src/Foreign/Rust/Marshall/Fixed.hs +++ b/src/Foreign/Rust/Marshall/Fixed.hs @@ -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) + diff --git a/src/Foreign/Rust/Marshall/Variable.hs b/src/Foreign/Rust/Marshall/Variable.hs index a84b591..a141e78 100644 --- a/src/Foreign/Rust/Marshall/Variable.hs +++ b/src/Foreign/Rust/Marshall/Variable.hs @@ -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 -------------------------------------------------------------------------------}