Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withForeignPtrST, touchForeignPtrST #294

Open
raehik opened this issue Oct 7, 2024 · 1 comment
Open

Add withForeignPtrST, touchForeignPtrST #294

raehik opened this issue Oct 7, 2024 · 1 comment

Comments

@raehik
Copy link

raehik commented Oct 7, 2024

withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b is a key utility for consuming ForeignPtrs safely. It is expectably common when consuming ByteStrings.
It and its related utilities use the primops

keepAlive# :: a -> State# s -> (State# s -> b) -> b
touch# :: a -> State# s -> State# s

But withForeignPtr is stuck in IO, preventing its use in ST contexts.
(This is probably due to the relevant primops being stuck in the RealWorld until #152 , implemented in GHC 9.8.)

I propose the following new functions:

withForeignPtrST :: ForeignPtr a -> (Ptr a -> ST s b) -> ST s b
touchForeignPtrST :: ForeignPtr a -> ST s ()

These would be defined in ghc-internal at GHC.Internal.ForeignPtr, together with their IO versions.

My draft implementation below is boring, copied from the existing IO versions with ST swapped in.

withForeignPtrST :: ForeignPtr a -> (Ptr a -> ST s b) -> ST s b
withForeignPtrST fo@(ForeignPtr _ r) f = ST $ \s ->
  case f (unsafeForeignPtrToPtr fo) of
    ST action# -> keepAlive# r s action#

touchForeignPtrST :: ForeignPtr a -> ST s ()
touchForeignPtrST (ForeignPtr _ r) = touchST r

-- not exported
touchST :: ForeignPtrContents -> ST s ()
touchST r = ST $ \s -> case touch# r s of s' -> (# s', () #)

No breaking changes.

@raehik
Copy link
Author

raehik commented Oct 7, 2024

I originally had to write these myself in order to consume ByteStrings inside a text-builder-linear primitive, which requests ST. @bgamari was kind enough to provide a cursory check of withForeignPtrST. See Bodigrim/linear-builder#23 (comment) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant