packages feed

hercules-ci-cnix-store-0.2.0.0: src/Hercules/CNix/Encapsulation.hs

{-# LANGUAGE FunctionalDependencies #-}

module Hercules.CNix.Encapsulation
  ( HasEncapsulation (..),
    nullableMoveToForeignPtrWrapper,
  )
where

import Foreign (Ptr, nullPtr)
import Prelude

class HasEncapsulation a b | b -> a where
  -- | Takes ownership of the pointer, freeing/finalizing the pointer when
  -- collectable.
  moveToForeignPtrWrapper :: Ptr a -> IO b

nullableMoveToForeignPtrWrapper :: HasEncapsulation a b => Ptr a -> IO (Maybe b)
nullableMoveToForeignPtrWrapper rawPtr | rawPtr == nullPtr = pure Nothing
nullableMoveToForeignPtrWrapper rawPtr = Just <$> moveToForeignPtrWrapper rawPtr