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:
parent
f30df93fb5
commit
7e18d48951
2 changed files with 19 additions and 31 deletions
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
-------------------------------------------------------------------------------}
|
-------------------------------------------------------------------------------}
|
||||||
|
|
Loading…
Reference in a new issue