-- | Storable @Vector@ unsafe functions. These perform no bounds
--   checking, and may cause segmentation faults etc.!  Import as:
--
-- > import qualified RIO.Vector.Storable.Unsafe as VS'
module RIO.Vector.Storable.Unsafe
  (
  -- * Accessors
  -- ** Indexing
    Data.Vector.Storable.unsafeIndex
  , Data.Vector.Storable.unsafeHead
  , Data.Vector.Storable.unsafeLast

  -- ** Monadic indexing
  , Data.Vector.Storable.unsafeIndexM
  , Data.Vector.Storable.unsafeHeadM
  , Data.Vector.Storable.unsafeLastM

  -- ** Extracting subvectors
  , Data.Vector.Storable.unsafeSlice
  , Data.Vector.Storable.unsafeInit
  , Data.Vector.Storable.unsafeTail
  , Data.Vector.Storable.unsafeTake
  , Data.Vector.Storable.unsafeDrop

  -- * Modifying vectors
  -- ** Bulk updates
  , Data.Vector.Storable.unsafeUpd
  , Data.Vector.Storable.unsafeUpdate_

  -- ** Accumulations
  , Data.Vector.Storable.unsafeAccum
  , Data.Vector.Storable.unsafeAccumulate_

  -- ** Permutations
  , Data.Vector.Storable.unsafeBackpermute

  -- * Conversions
  -- ** Mutable vectors
  , Data.Vector.Storable.unsafeFreeze
  , Data.Vector.Storable.unsafeThaw
  , Data.Vector.Storable.unsafeCopy

  -- * Raw pointers
  , Data.Vector.Storable.unsafeFromForeignPtr
  , Data.Vector.Storable.unsafeFromForeignPtr0
  , Data.Vector.Storable.unsafeToForeignPtr
  , Data.Vector.Storable.unsafeToForeignPtr0
  , unsafeWith
  ) where

import Data.Vector.Storable(Storable, Vector)
import qualified Data.Vector.Storable
import Foreign.Ptr(Ptr)
import UnliftIO

-- | Lifted version of 'Data.Vector.Storable.unsafeWith'
unsafeWith :: (MonadUnliftIO m, Storable a) => Vector a -> (Ptr a -> m b) -> m b
unsafeWith :: Vector a -> (Ptr a -> m b) -> m b
unsafeWith Vector a
vec Ptr a -> m b
action = ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
unlifter -> Vector a -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
Data.Vector.Storable.unsafeWith Vector a
vec (m b -> IO b
forall a. m a -> IO a
unlifter (m b -> IO b) -> (Ptr a -> m b) -> Ptr a -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr a -> m b
action)