Skip to content

Commit 60692d6

Browse files
committed
Use Number for reading and writing, fixes #25
Int is not suitable for many of the options for BufferValueType, since both UInt32 types as well as all of the floating point types are inhabited by values which are not representable as Int. This is quite a big breaking change unfortunately, but the library is basically broken for about half of the BufferValueType constructors right now, and this seems to me to be the most sensible way of fixing it. Note that {to,from}Array are unchanged because they deal with arrays of octets.
1 parent 7098df6 commit 60692d6

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

src/Node/Buffer/Class.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Monad m <= MutableBuffer buf m | buf -> m where
6868
toArrayBuffer :: buf -> m ArrayBuffer
6969

7070
-- | Reads a numeric value from a buffer at the specified offset.
71-
read :: BufferValueType -> Offset -> buf -> m Int
71+
read :: BufferValueType -> Offset -> buf -> m Number
7272

7373
-- | Reads a section of a buffer as a string with the specified encoding.
7474
readString :: Encoding -> Offset -> Offset -> buf -> m String
@@ -77,7 +77,7 @@ class Monad m <= MutableBuffer buf m | buf -> m where
7777
toString :: Encoding -> buf -> m String
7878

7979
-- | Writes a numeric value to a buffer at the specified offset.
80-
write :: BufferValueType -> Int -> Offset -> buf -> m Unit
80+
write :: BufferValueType -> Number -> Offset -> buf -> m Unit
8181

8282
-- | Writes octets from a string to a buffer at the specified offset. Multi-byte
8383
-- | characters will not be written to the buffer if there is not enough capacity

src/Node/Buffer/Immutable.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ fromString str = fromStringImpl str <<< encodingToNode
6464
foreign import fromStringImpl :: String -> String -> ImmutableBuffer
6565

6666
-- | Reads a numeric value from a buffer at the specified offset.
67-
read :: BufferValueType -> Offset -> ImmutableBuffer -> Int
67+
read :: BufferValueType -> Offset -> ImmutableBuffer -> Number
6868
read = readImpl <<< show
6969

70-
foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Int
70+
foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Number
7171

7272
-- | Reads a section of a buffer as a string with the specified encoding.
7373
readString :: Encoding -> Offset -> Offset -> ImmutableBuffer -> String

src/Node/Buffer/Internal.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fromArrayBuffer = usingToImmutable Immutable.fromArrayBuffer
6464
toArrayBuffer :: forall buf m. Monad m => buf -> m ArrayBuffer
6565
toArrayBuffer = usingFromImmutable Immutable.toArrayBuffer
6666

67-
read :: forall buf m. Monad m => BufferValueType -> Offset -> buf -> m Int
67+
read :: forall buf m. Monad m => BufferValueType -> Offset -> buf -> m Number
6868
read t o = usingFromImmutable $ Immutable.read t o
6969

7070
readString :: forall buf m. Monad m => Encoding -> Offset -> Offset -> buf -> m String
@@ -73,10 +73,10 @@ readString m o o' = usingFromImmutable $ Immutable.readString m o o'
7373
toString :: forall buf m. Monad m => Encoding -> buf -> m String
7474
toString m = usingFromImmutable $ Immutable.toString m
7575

76-
write :: forall buf m. Monad m => BufferValueType -> Int -> Offset -> buf -> m Unit
76+
write :: forall buf m. Monad m => BufferValueType -> Number -> Offset -> buf -> m Unit
7777
write = writeInternal <<< show
7878

79-
foreign import writeInternal :: forall buf m. String -> Int -> Offset -> buf -> m Unit
79+
foreign import writeInternal :: forall buf m. String -> Number -> Offset -> buf -> m Unit
8080

8181
writeString :: forall buf m. Monad m => Encoding -> Offset -> Int -> String -> buf -> m Int
8282
writeString = writeStringInternal <<< encodingToNode

test/Test/Node/Buffer/Class.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ testMutableBuffer _ run = do
8080

8181
testReadWrite :: Effect Unit
8282
testReadWrite = do
83-
let val = 42
83+
let val = 42.0
8484
readVal <- run do
8585
buf <- create 1 :: m buf
8686
write UInt8 val 0 buf
@@ -94,7 +94,7 @@ testMutableBuffer _ run = do
9494
buf <- fromArray [1,2,3,4,5] :: m buf
9595
read UInt8 2 buf
9696

97-
assertEqual {expected: 3, actual: readVal}
97+
assertEqual {expected: 3.0, actual: readVal}
9898

9999
testToArray :: Effect Unit
100100
testToArray = do
@@ -112,7 +112,7 @@ testMutableBuffer _ run = do
112112
buf <- fromString str ASCII :: m buf
113113
read UInt8 6 buf
114114

115-
assertEqual {expected: 32, actual: val} -- ASCII space
115+
assertEqual {expected: 32.0, actual: val} -- ASCII space
116116

117117
testToFromArrayBuffer :: Effect Unit
118118
testToFromArrayBuffer = do

test/Test/Node/Buffer/Immutable.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ testCreate = do
8282
testFromString :: Effect Unit
8383
testFromString = do
8484
let buf = Immutable.fromString "hello, world" ASCII
85-
assertEqual {expected: 32, actual: Immutable.read UInt8 6 buf}
85+
assertEqual {expected: 32.0, actual: Immutable.read UInt8 6 buf}
8686

8787
testToString :: Effect Unit
8888
testToString = do

0 commit comments

Comments
 (0)