/
ST.purs
47 lines (42 loc) · 1.44 KB
/
ST.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Node.Buffer.ST
( STBuffer
, run
) where
import Prelude
import Control.Monad.ST (ST, kind Region)
import Control.Monad.ST as ST
import Node.Buffer.Class (class MutableBuffer, unsafeFreeze)
import Node.Buffer.Immutable (ImmutableBuffer)
import Node.Buffer.Internal as Internal
-- | A reference to a mutable buffer for use with `ST`
-- |
-- | The type parameter represents the memory region which the buffer belongs to.
foreign import data STBuffer :: Region -> Type
-- | Runs an effect creating an `STBuffer` then freezes the buffer and returns
-- | it, without unneccessary copying.
run :: (forall h. ST h (STBuffer h)) -> ImmutableBuffer
run st = ST.run (st >>= unsafeFreeze)
instance mutableBufferST :: MutableBuffer (STBuffer h) (ST h) where
create = Internal.create
freeze = Internal.copyAll
unsafeFreeze = Internal.unsafeFreeze
thaw = Internal.copyAll
unsafeThaw = Internal.unsafeThaw
fromArray = Internal.fromArray
fromString = Internal.fromString
fromArrayBuffer = Internal.fromArrayBuffer
toArrayBuffer = Internal.toArrayBuffer
read = Internal.read
readString = Internal.readString
toString = Internal.toString
write = Internal.write
writeString = Internal.writeString
toArray = Internal.toArray
getAtOffset = Internal.getAtOffset
setAtOffset = Internal.setAtOffset
slice = Internal.slice
size = Internal.size
concat = Internal.concat
concat' = Internal.concat'
copy = Internal.copy
fill = Internal.fill