Skip to content

Commit 9b48b29

Browse files
committed
Add Monad constraint and rename type variables on MutableBuffer class.
As requested in #discussion_r244534678
1 parent efcd6a4 commit 9b48b29

File tree

4 files changed

+69
-69
lines changed

4 files changed

+69
-69
lines changed

src/Node/Buffer/Mutable.purs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,74 +37,74 @@ import Node.Buffer as Buffer
3737
import Node.Encoding (Encoding, encodingToNode)
3838
import Unsafe.Coerce (unsafeCoerce)
3939

40-
-- | A type class for mutable buffers `b` where operations on those buffers are
41-
-- | represented by a particular effect type `e`.
42-
class MutableBuffer b e | e -> b, b -> e where
40+
-- | A type class for mutable buffers `buf` where operations on those buffers are
41+
-- | represented by a particular monadic effect type `m`.
42+
class Monad m <= MutableBuffer buf m | m -> buf, buf -> m where
4343

4444
-- | Creates a new buffer of the specified size.
45-
create :: Int -> e b
45+
create :: Int -> m buf
4646

4747
-- | Creates an immutable copy of a mutable buffer.
48-
freeze :: b -> e Buffer
48+
freeze :: buf -> m Buffer
4949

5050
-- | Creates a mutable copy of an immutable buffer.
51-
thaw :: Buffer -> e b
51+
thaw :: Buffer -> m buf
5252

5353
-- | Creates a new buffer from an array of octets, sized to match the array.
54-
fromArray :: Array Octet -> e b
54+
fromArray :: Array Octet -> m buf
5555

5656
-- | Creates a new buffer from a string with the specified encoding, sized to
5757
-- | match the string.
58-
fromString :: String -> Encoding -> e b
58+
fromString :: String -> Encoding -> m buf
5959

6060
-- | Creates a buffer view from a JS ArrayByffer without copying data.
61-
fromArrayBuffer :: ArrayBuffer -> e b
61+
fromArrayBuffer :: ArrayBuffer -> m buf
6262

6363
-- | Copies the data in the buffer to a new JS ArrayBuffer
64-
toArrayBuffer :: b -> e ArrayBuffer
64+
toArrayBuffer :: buf -> m ArrayBuffer
6565

6666
-- | Reads a numeric value from a buffer at the specified offset.
67-
read :: BufferValueType -> Offset -> b -> e Int
67+
read :: BufferValueType -> Offset -> buf -> m Int
6868

6969
-- | Reads a section of a buffer as a string with the specified encoding.
70-
readString :: Encoding -> Offset -> Offset -> b -> e String
70+
readString :: Encoding -> Offset -> Offset -> buf -> m String
7171

7272
-- | Reads the buffer as a string with the specified encoding.
73-
toString :: Encoding -> b -> e String
73+
toString :: Encoding -> buf -> m String
7474

7575
-- | Writes a numeric value to a buffer at the specified offset.
76-
write :: BufferValueType -> Int -> Offset -> b -> e Unit
76+
write :: BufferValueType -> Int -> Offset -> buf -> m Unit
7777

7878
-- | Writes octets from a string to a buffer at the specified offset. Multi-byte
7979
-- | characters will not be written to the buffer if there is not enough capacity
8080
-- | to write them fully. The number of bytes written is returned.
81-
writeString :: Encoding -> Offset -> Int -> String -> b -> e Int
81+
writeString :: Encoding -> Offset -> Int -> String -> buf -> m Int
8282

8383
-- | Creates an array of octets from a buffer's contents.
84-
toArray :: b -> e (Array Octet)
84+
toArray :: buf -> m (Array Octet)
8585

8686
-- | Reads an octet from a buffer at the specified offset.
87-
getAtOffset :: Offset -> b -> e (Maybe Octet)
87+
getAtOffset :: Offset -> buf -> m (Maybe Octet)
8888

8989
-- | Writes an octet in the buffer at the specified offset.
90-
setAtOffset :: Octet -> Offset -> b -> e Unit
90+
setAtOffset :: Octet -> Offset -> buf -> m Unit
9191

9292
-- | Returns the size of a buffer.
93-
size :: b -> e Int
93+
size :: buf -> m Int
9494

9595
-- | Concatenates a list of buffers.
96-
concat :: Array b -> e b
96+
concat :: Array buf -> m buf
9797

9898
-- | Concatenates a list of buffers, combining them into a new buffer of the
9999
-- | specified length.
100-
concat' :: Array b -> Int -> e b
100+
concat' :: Array buf -> Int -> m buf
101101

102102
-- | Copies a section of a source buffer into a target buffer at the specified
103103
-- | offset, and returns the number of octets copied.
104-
copy :: Offset -> Offset -> b -> Offset -> b -> e Int
104+
copy :: Offset -> Offset -> buf -> Offset -> buf -> m Int
105105

106106
-- | Fills a range in a buffer with the specified octet.
107-
fill :: Octet -> Offset -> Offset -> b -> e Unit
107+
fill :: Octet -> Offset -> Offset -> buf -> m Unit
108108

109109
-- | A reference to a mutable buffer for use with `Effect`
110110
foreign import data EffectBuffer :: Type
@@ -163,66 +163,66 @@ instance mutableBufferST :: MutableBuffer (STBuffer h) (ST h) where
163163
copy = copyImpl
164164
fill = fillImpl
165165

166-
usingFromFrozen :: forall b e a. (Buffer -> a) -> b -> e a
166+
usingFromFrozen :: forall buf m a. (Buffer -> a) -> buf -> m a
167167
usingFromFrozen f buf = unsafeCoerce \_ -> f $ unsafeCoerce buf
168168

169-
usingToFrozen :: forall b e a. (a -> Buffer) -> a -> e b
169+
usingToFrozen :: forall buf m a. (a -> Buffer) -> a -> m buf
170170
usingToFrozen f x = unsafeCoerce \_ -> unsafeCoerce $ f x
171171

172-
createImpl :: forall b e. Int -> e b
172+
createImpl :: forall buf m. Int -> m buf
173173
createImpl = usingToFrozen Buffer.create
174174

175-
foreign import copyAllImpl :: forall a b e. a -> e b
175+
foreign import copyAllImpl :: forall a buf m. a -> m buf
176176

177-
fromArrayImpl :: forall b e. Array Octet -> e b
177+
fromArrayImpl :: forall buf m. Array Octet -> m buf
178178
fromArrayImpl = usingToFrozen Buffer.fromArray
179179

180-
fromStringImpl :: forall b e. String -> Encoding -> e b
180+
fromStringImpl :: forall buf m. String -> Encoding -> m buf
181181
fromStringImpl s = usingToFrozen $ Buffer.fromString s
182182

183-
fromArrayBufferImpl :: forall b e. ArrayBuffer -> e b
183+
fromArrayBufferImpl :: forall buf m. ArrayBuffer -> m buf
184184
fromArrayBufferImpl = usingToFrozen Buffer.fromArrayBuffer
185185

186-
toArrayBufferImpl :: forall b e. b -> e ArrayBuffer
186+
toArrayBufferImpl :: forall buf m. buf -> m ArrayBuffer
187187
toArrayBufferImpl = usingFromFrozen Buffer.toArrayBuffer
188188

189-
readImpl :: forall b e. BufferValueType -> Offset -> b -> e Int
189+
readImpl :: forall buf m. BufferValueType -> Offset -> buf -> m Int
190190
readImpl t o = usingFromFrozen $ Buffer.read t o
191191

192-
readStringImpl :: forall b e. Encoding -> Offset -> Offset -> b -> e String
193-
readStringImpl e o o' = usingFromFrozen $ Buffer.readString e o o'
192+
readStringImpl :: forall buf m. Encoding -> Offset -> Offset -> buf -> m String
193+
readStringImpl m o o' = usingFromFrozen $ Buffer.readString m o o'
194194

195-
toStringImpl :: forall b e. Encoding -> b -> e String
196-
toStringImpl e = usingFromFrozen $ Buffer.toString e
195+
toStringImpl :: forall buf m. Encoding -> buf -> m String
196+
toStringImpl m = usingFromFrozen $ Buffer.toString m
197197

198-
writeImpl :: forall b e. BufferValueType -> Int -> Offset -> b -> e Unit
198+
writeImpl :: forall buf m. BufferValueType -> Int -> Offset -> buf -> m Unit
199199
writeImpl = writeInternal <<< show
200200

201-
foreign import writeInternal :: forall b e. String -> Int -> Offset -> b -> e Unit
201+
foreign import writeInternal :: forall buf m. String -> Int -> Offset -> buf -> m Unit
202202

203-
writeStringImpl :: forall b e. Encoding -> Offset -> Int -> String -> b -> e Int
203+
writeStringImpl :: forall buf m. Encoding -> Offset -> Int -> String -> buf -> m Int
204204
writeStringImpl = writeStringInternal <<< encodingToNode
205205

206206
foreign import writeStringInternal ::
207-
forall b e. String -> Offset -> Int -> String -> b -> e Int
207+
forall buf m. String -> Offset -> Int -> String -> buf -> m Int
208208

209-
toArrayImpl :: forall b e. b -> e (Array Octet)
209+
toArrayImpl :: forall buf m. buf -> m (Array Octet)
210210
toArrayImpl = usingFromFrozen Buffer.toArray
211211

212-
getAtOffsetImpl :: forall b e. Offset -> b -> e (Maybe Octet)
212+
getAtOffsetImpl :: forall buf m. Offset -> buf -> m (Maybe Octet)
213213
getAtOffsetImpl o = usingFromFrozen $ Buffer.getAtOffset o
214214

215-
foreign import setAtOffsetImpl :: forall b e. Octet -> Offset -> b -> e Unit
215+
foreign import setAtOffsetImpl :: forall buf m. Octet -> Offset -> buf -> m Unit
216216

217-
sizeImpl :: forall b e. b -> e Int
217+
sizeImpl :: forall buf m. buf -> m Int
218218
sizeImpl = usingFromFrozen Buffer.size
219219

220-
concatImpl :: forall b e. Array b -> e b
220+
concatImpl :: forall buf m. Array buf -> m buf
221221
concatImpl arrs = unsafeCoerce \_ -> Buffer.concat (unsafeCoerce arrs)
222222

223-
concatImpl' :: forall b e. Array b -> Int -> e b
223+
concatImpl' :: forall buf m. Array buf -> Int -> m buf
224224
concatImpl' arrs n = unsafeCoerce \_ -> Buffer.concat' (unsafeCoerce arrs) n
225225

226-
foreign import copyImpl :: forall b e. Offset -> Offset -> b -> Offset -> b -> e Int
226+
foreign import copyImpl :: forall buf m. Offset -> Offset -> buf -> Offset -> buf -> m Int
227227

228-
foreign import fillImpl :: forall b e. Octet -> Offset -> Offset -> b -> e Unit
228+
foreign import fillImpl :: forall buf m. Octet -> Offset -> Offset -> buf -> m Unit

src/Node/Buffer/Mutable/Unsafe.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import Node.Buffer (Offset)
1111
import Node.Buffer.Mutable (class MutableBuffer, EffectBuffer, STBuffer)
1212
import Unsafe.Coerce (unsafeCoerce)
1313

14-
class MutableBuffer b e <= MutableBufferUnsafe b e | b -> e, e -> b where
14+
class MutableBuffer buf m <= MutableBufferUnsafe buf m | buf -> m, m -> buf where
1515

1616
-- | Creates a new buffer slice that acts like a window on the original buffer.
1717
-- | Writing to the slice buffer updates the original buffer and vice-versa.
1818
-- |
1919
-- | This is considered unsafe as writing to a slice can result in action at a
2020
-- | distance.
21-
slice :: Offset -> Offset -> b -> b
21+
slice :: Offset -> Offset -> buf -> buf
2222

2323
instance mutableBufferUnsafeEffect :: MutableBufferUnsafe EffectBuffer Effect where
2424
slice = sliceImpl
2525

2626
instance mutableBufferUnsafeST :: MutableBufferUnsafe (STBuffer h) (ST h) where
2727
slice = sliceImpl
2828

29-
sliceImpl :: forall b. Offset -> Offset -> b -> b
29+
sliceImpl :: forall buf. Offset -> Offset -> buf -> buf
3030
sliceImpl = unsafeCoerce Buffer.slice

test/Test/Node/Buffer/Mutable.purs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ test = do
2929

3030
Unsafe.test
3131

32-
testMutableBuffer :: forall b e. Monad e => MutableBuffer b e =>
33-
Proxy b -> (forall a. e a -> Effect a) -> Effect Unit
32+
testMutableBuffer :: forall buf m. MutableBuffer buf m =>
33+
Proxy buf -> (forall a. m a -> Effect a) -> Effect Unit
3434
testMutableBuffer _ run = do
3535

3636
log " - create"
@@ -78,24 +78,24 @@ testMutableBuffer _ run = do
7878
where
7979
testCreate :: Effect Unit
8080
testCreate = do
81-
buf <- run ((create 3 :: e b) >>= toArray)
81+
buf <- run ((create 3 :: m buf) >>= toArray)
8282
assertEqual {expected: [0, 0, 0], actual: buf}
8383

8484
testFreeze :: Effect Unit
8585
testFreeze = do
86-
buf <- Buffer.toArray <$> run ((fromArray [1, 2, 3] :: e b) >>= freeze)
86+
buf <- Buffer.toArray <$> run ((fromArray [1, 2, 3] :: m buf) >>= freeze)
8787
assertEqual {expected: [1, 2, 3], actual: buf}
8888

8989
testThaw :: Effect Unit
9090
testThaw = do
91-
buf <- run ((thaw (Buffer.fromArray [1, 2, 3]) :: e b) >>= toArray)
91+
buf <- run ((thaw (Buffer.fromArray [1, 2, 3]) :: m buf) >>= toArray)
9292
assertEqual {expected: [1, 2, 3], actual: buf}
9393

9494
testReadWrite :: Effect Unit
9595
testReadWrite = do
9696
let val = 42
9797
readVal <- run do
98-
buf <- create 1 :: e b
98+
buf <- create 1 :: m buf
9999
write UInt8 val 0 buf
100100
read UInt8 0 buf
101101

@@ -104,7 +104,7 @@ testMutableBuffer _ run = do
104104
testFromArray :: Effect Unit
105105
testFromArray = do
106106
readVal <- run do
107-
buf <- fromArray [1,2,3,4,5] :: e b
107+
buf <- fromArray [1,2,3,4,5] :: m buf
108108
read UInt8 2 buf
109109

110110
assertEqual {expected: 3, actual: readVal}
@@ -113,7 +113,7 @@ testMutableBuffer _ run = do
113113
testToArray = do
114114
let val = [1,2,67,3,3,7,8,3,4,237]
115115
valOut <- run do
116-
buf <- fromArray val :: e b
116+
buf <- fromArray val :: m buf
117117
toArray buf
118118

119119
assertEqual {expected: val, actual: valOut}
@@ -122,7 +122,7 @@ testMutableBuffer _ run = do
122122
testFromString = do
123123
let str = "hello, world"
124124
val <- run do
125-
buf <- fromString str ASCII :: e b
125+
buf <- fromString str ASCII :: m buf
126126
read UInt8 6 buf
127127

128128
assertEqual {expected: 32, actual: val} -- ASCII space
@@ -140,7 +140,7 @@ testMutableBuffer _ run = do
140140
testToString = do
141141
let str = "hello, world"
142142
strOut <-run do
143-
buf <- fromString str ASCII :: e b
143+
buf <- fromString str ASCII :: m buf
144144
toString ASCII buf
145145

146146
assertEqual {expected: str, actual: strOut}
@@ -149,15 +149,15 @@ testMutableBuffer _ run = do
149149
testReadString = do
150150
let str = "hello, world"
151151
strOut <- run do
152-
buf <- fromString str ASCII :: e b
152+
buf <- fromString str ASCII :: m buf
153153
readString ASCII 7 12 buf
154154

155155
assertEqual {expected: "world", actual: strOut}
156156

157157
testCopy :: Effect Unit
158158
testCopy = do
159159
{copied, out} <- run do
160-
buf1 <- fromArray [1,2,3,4,5] :: e b
160+
buf1 <- fromArray [1,2,3,4,5] :: m buf
161161
buf2 <- fromArray [10,9,8,7,6]
162162
copied <- copy 0 3 buf1 2 buf2
163163
out <- toArray buf2
@@ -179,15 +179,15 @@ testMutableBuffer _ run = do
179179
testConcat' = do
180180
out <- run do
181181
bufs <- traverse fromArray $ map (\x -> [x, x+1, x+2]) [0,3,6,9,12]
182-
buf <- concat' bufs 15 :: e b
182+
buf <- concat' bufs 15 :: m buf
183183
toArray buf
184184

185185
assertEqual {expected: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14], actual: out}
186186

187187
testGetAtOffset :: Effect Unit
188188
testGetAtOffset = do
189189
{o1, o4, om1} <- run do
190-
buf <- fromArray [1, 2, 3, 4] :: e b
190+
buf <- fromArray [1, 2, 3, 4] :: m buf
191191
o1 <- getAtOffset 1 buf
192192
o4 <- getAtOffset 4 buf
193193
om1 <- getAtOffset (-1) buf

test/Test/Node/Buffer/Mutable/Unsafe.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ test = do
1919
log "Testing Node.Buffer.Mutable.Unsafe [STBuffer] ..."
2020
testMutableBufferUnsafe (Proxy :: Proxy (STBuffer _)) (unsafeCoerce ST.run >>> pure)
2121

22-
testMutableBufferUnsafe :: forall b e. Monad e => MutableBufferUnsafe b e =>
23-
Proxy b -> (forall a. e a -> Effect a) -> Effect Unit
22+
testMutableBufferUnsafe :: forall buf m. MutableBufferUnsafe buf m =>
23+
Proxy buf -> (forall a. m a -> Effect a) -> Effect Unit
2424
testMutableBufferUnsafe _ run = do
2525

2626
log " - slice"
@@ -30,8 +30,8 @@ testMutableBufferUnsafe _ run = do
3030
testSlice :: Effect Unit
3131
testSlice = do
3232
{bufArr, bufSliceArr} <- run do
33-
buf <- fromArray [1, 2, 3, 4] :: e b
34-
let bufSlice = slice 1 3 buf :: b
33+
buf <- fromArray [1, 2, 3, 4] :: m buf
34+
let bufSlice = slice 1 3 buf :: buf
3535
setAtOffset 42 1 bufSlice
3636
bufArr <- toArray buf
3737
bufSliceArr <- toArray bufSlice

0 commit comments

Comments
 (0)