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 unsafeCast to Primitive vectors #401

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vector/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
resulted in an error. This change was introduced in:
[#382](https://github.com/haskell/vector/pull/382)
* Remove redundant `Storable` constraints on to/from `ForeignPtr` conversions
* Add `unsafeCast` to `Primitive` vectors

# Changes in version 0.12.3.0

Expand Down
21 changes: 19 additions & 2 deletions vector/src/Data/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module Data.Vector.Primitive (
toList, fromList, fromListN,

-- ** Other vector types
G.convert,
G.convert, unsafeCast,
unsafeCoerceVector,

-- ** Mutable vectors
Expand All @@ -157,6 +157,7 @@ module Data.Vector.Primitive (

import qualified Data.Vector.Generic as G
import Data.Vector.Primitive.Mutable ( MVector(..) )
import Data.Vector.Internal.Check
import qualified Data.Vector.Fusion.Bundle as Bundle
import Data.Primitive.ByteArray
import Data.Primitive ( Prim, sizeOf )
Expand Down Expand Up @@ -203,7 +204,7 @@ type role Vector nominal
-- This is marginally safer than 'unsafeCast', since this function imposes an
-- extra 'Coercible' constraint. This function is still not safe, however,
-- since it cannot guarantee that the two types have memory-compatible
-- 'Storable' instances.
-- 'Prim' instances.
--
-- Note that this function is unsafe. The @Coercible@ constraint guarantees
-- that the element types are representationally equal. It however cannot
Expand Down Expand Up @@ -1769,6 +1770,22 @@ fromListN :: Prim a => Int -> [a] -> Vector a
{-# INLINE fromListN #-}
fromListN = G.fromListN

-- Conversions - Unsafe casts
-- --------------------------

-- | /O(1)/ Unsafely cast a vector from one element type to another.
-- This operation just changes the type of the vector and does not
-- modify the elements.
--
-- This function will throw an error if elements are of mismatching sizes.
--
-- | @since 0.13.0.0
unsafeCast :: forall a b. (HasCallStack, Prim a, Prim b) => Vector a -> Vector b
{-# INLINE unsafeCast #-}
unsafeCast (Vector o n ba)
| sizeOf (undefined :: a) == sizeOf (undefined :: b) = Vector o n ba
| otherwise = error "Element size mismatch"

-- Conversions - Mutable vectors
-- -----------------------------

Expand Down
20 changes: 19 additions & 1 deletion vector/src/Data/Vector/Primitive/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ module Data.Vector.Primitive.Mutable (
set, copy, move, unsafeCopy, unsafeMove,

-- * Unsafe conversions
unsafeCoerceMVector,
unsafeCoerceMVector, unsafeCast,
-- * Re-exports
Prim, PrimMonad, PrimState, RealWorld
) where

import qualified Data.Vector.Generic.Mutable as G
import Data.Primitive.ByteArray
import Data.Primitive ( Prim, sizeOf )
import Data.Vector.Internal.Check
import Data.Word ( Word8 )
import Control.Monad.Primitive
import Control.Monad ( liftM )
Expand Down Expand Up @@ -656,3 +657,20 @@ ifoldrM = G.ifoldrM
ifoldrM' :: (PrimMonad m, Prim a) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m) a -> m b
{-# INLINE ifoldrM' #-}
ifoldrM' = G.ifoldrM'


-- Unsafe conversions
-- ------------------

-- | /O(1)/ Unsafely cast a vector from one element type to another.
-- This operation just changes the type of the vector and does not
-- modify the elements.
--
-- This function will throw an error if elements are of mismatching sizes.
--
-- | @since 0.13.0.0
unsafeCast :: forall a b s. (HasCallStack, Prim a, Prim b) => MVector s a -> MVector s b
{-# INLINE unsafeCast #-}
unsafeCast (MVector o n ba)
| sizeOf (undefined :: a) == sizeOf (undefined :: b) = MVector o n ba
| otherwise = error "Element size mismatch"