Skip to content

Find a safe way to convert an GHC.Exts.Array# to a Data.Vector.Vector #214

@utdemir

Description

@utdemir

When converting our mutable array to a vector (#205), we end up having a GHC.Exts.Array#. Which happens to be te same type Data.Vector.Vector is made of, but we can not use it since its constructor is hidden.

Instead, we have to go through a roundabaut way; from Array# to MutableArray# to MVector to Vector; all using some kind of unsafe calls.

-- | /O(1)/ Convert an 'Array' to an immutable 'Vector.Vector' (from
-- 'vector' package).
freeze :: Array a #-> Ur (Vector.Vector a)
freeze (Array arr) =
Unlifted.freeze go arr
where
go arr = unsafeDupablePerformIO $ do
mut <- Prim.unsafeThawArray (Prim.Array arr)
let mv = MVector.MVector 0 (Prim.sizeofMutableArray mut) mut
Vector.unsafeFreeze mv
-- We only need to do above because 'Vector' constructor is hidden.
-- Once it is exposed, we should be able to replace it with something
-- safer like: `go arr = Vector 0 (sizeof arr) arr`

Making Vector's constructor visible would solve this easily, so we should consider contributing upstream.

Related upstream issue: haskell/vector#171

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions