Ticket #30 (closed defect: worksforme)

Opened 3 years ago

Last modified 3 years ago

There is no safe analog of unsafeFreeze

Reported by: anonymous Owned by:
Priority: major Milestone:
Version: Keywords:
Cc: alexey.skladnoy@…

Description

Summary is quite exhaustive here. Such function is required when one needs snapshot of mutable vector which will be mutated afterwards. Improper usage of unsafeFreeze already caused bug in mwc-random.

Here is safeFreeze I used:

-- Safe version of unsafeFreeze.
safeFreeze :: (PrimMonad m, Vector v a) => G.Mutable v (PrimState m) a -> m (v a)
safeFreeze v = do
  v' <- GM.unsafeNew (GM.length v)
  GM.unsafeCopy v' v
  unsafeFreeze v'

Change History

Changed 3 years ago by rl

  • status changed from new to closed
  • resolution set to worksforme

Happily, the current darcs version already has this:

-- | /O(n)/ Yield an immutable copy of the mutable vector.
freeze :: (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m (v a)
{-# INLINE freeze #-}
freeze mv = unsafeFreeze =<< M.clone mv
Note: See TracTickets for help on using tickets.