Skip to content

Commit f011f98

Browse files
committed
Use plain functions instead of type class instances
1 parent e208591 commit f011f98

File tree

3 files changed

+23
-61
lines changed

3 files changed

+23
-61
lines changed

src/Data/ArrayBuffer/Deserializer.purs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ import qualified Data.ArrayBuffer.DataView as DV
1111

1212
type Deserializer = Advancer
1313

14-
class IsDeserializable a where
15-
get :: forall e. Deserializer -> Eff (reader :: DV.Reader | e) (Either String a)
16-
1714
chkErr :: forall a e. Maybe a -> Either String a
1815
chkErr x = case x of
1916
(Just v) -> Right v
2017
otherwise -> Left "Short read"
2118

22-
2319
getInt8 :: forall e. Deserializer -> Eff (reader :: DV.Reader | e) (Either String Int8)
2420
getInt8 d = chkErr <$> (advance 1 d >>= DV.getInt8 d.dv)
2521
getInt16 d = chkErr <$> (advance 2 d >>= DV.getInt16 d.dv)
@@ -30,23 +26,6 @@ getUint32 d = chkErr <$> (advance 4 d >>= DV.getUint32 d.dv)
3026
getFloat32 d = chkErr <$> (advance 4 d >>= DV.getFloat32 d.dv)
3127
getFloat64 d = chkErr <$> (advance 8 d >>= DV.getFloat64 d.dv)
3228

33-
instance isDeserializableInt8 :: IsDeserializable Int8 where
34-
get = getInt8
35-
instance isDeserializableInt16 :: IsDeserializable Int16 where
36-
get = getInt16
37-
instance isDeserializableInt32 :: IsDeserializable Int32 where
38-
get = getInt32
39-
instance isDeserializableUint8 :: IsDeserializable Uint8 where
40-
get = getUint8
41-
instance isDeserializableUint16 :: IsDeserializable Uint16 where
42-
get = getUint16
43-
instance isDeserializableUint32 :: IsDeserializable Uint32 where
44-
get = getUint32
45-
instance isDeserializableFloat32 :: IsDeserializable Float32 where
46-
get = getFloat32
47-
instance isDeserializableFloat64 :: IsDeserializable Float64 where
48-
get = getFloat64
49-
5029
getDataView :: forall e. Deserializer -> ByteLength -> Eff (reader :: DV.Reader | e) (Either String DV.DataView)
5130
getDataView d n = do
5231
o <- advance n d
@@ -64,7 +43,6 @@ getTypedArray d sz conv = do
6443
Right dv -> Right $ conv dv
6544
Left err -> Left err
6645

67-
6846
getInt8Array :: AD TA.Int8Array
6947
getInt8Array d n = getTypedArray d n TA.asInt8Array
7048
getInt16Array :: AD TA.Int16Array

src/Data/ArrayBuffer/Serializer.purs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,14 @@ import Control.Monad.Eff
1010

1111
type Serializer = Advancer
1212

13-
class IsSerializable a where
14-
put :: forall e. Serializer -> a -> Eff (writer :: DV.Writer | e) Unit
15-
16-
instance isSerializableInt8 :: IsSerializable Int8 where
17-
put s v = advance 1 s >>= DV.setInt8 s.dv v
18-
instance isSerializableInt16 :: IsSerializable Int16 where
19-
put s v = advance 2 s >>= DV.setInt16 s.dv v
20-
instance isSerializableInt32 :: IsSerializable Int32 where
21-
put s v = advance 4 s >>= DV.setInt32 s.dv v
22-
instance isSerializableUint8 :: IsSerializable Uint8 where
23-
put s v = advance 1 s >>= DV.setUint8 s.dv v
24-
instance isSerializableUint16 :: IsSerializable Uint16 where
25-
put s v = advance 2 s >>= DV.setUint16 s.dv v
26-
instance isSerializableUint32 :: IsSerializable Uint32 where
27-
put s v = advance 4 s >>= DV.setUint32 s.dv v
28-
instance isSerializableFloat32 :: IsSerializable Float32 where
29-
put s v = advance 4 s >>= DV.setFloat32 s.dv v
30-
instance isSerializableFloat64 :: IsSerializable Float64 where
31-
put s v = advance 8 s >>= DV.setFloat64 s.dv v
13+
putInt8 s v = advance 1 s >>= DV.setInt8 s.dv v
14+
putInt16 s v = advance 2 s >>= DV.setInt16 s.dv v
15+
putInt32 s v = advance 4 s >>= DV.setInt32 s.dv v
16+
putUint8 s v = advance 1 s >>= DV.setUint8 s.dv v
17+
putUint16 s v = advance 2 s >>= DV.setUint16 s.dv v
18+
putUint32 s v = advance 4 s >>= DV.setUint32 s.dv v
19+
putFloat32 s v = advance 4 s >>= DV.setFloat32 s.dv v
20+
putFloat64 s v = advance 8 s >>= DV.setFloat64 s.dv v
3221

3322
mapDataView :: forall e. Serializer -> ByteLength -> Eff (writer :: DV.Writer | e) (Maybe DV.DataView)
3423
mapDataView s n = do

test/Main.purs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import Math
1818

1919
newtype Comp = Comp Number
2020

21-
instance isSerializableComp :: S.IsSerializable Comp where
22-
put d (Comp v) = S.put d $ Int8 v
21+
putComp d (Comp v) = S.putInt8 d $ Int8 v
2322

24-
instance isDeserializableComp :: D.IsDeserializable Comp where
25-
get d = do
23+
24+
getComp d = do
2625
v <- D.getInt8 d
2726
return $ case v of
2827
(Right (Int8 vv)) -> Right $ Comp vv
@@ -52,17 +51,15 @@ instance showV4 :: Show V4 where
5251
instance arbV4 :: Arbitrary V4 where
5352
arbitrary = V4 <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
5453

55-
instance v4IsSerializable :: S.IsSerializable V4 where
56-
put d (V4 x y z t) = do
57-
let i8 = S.put d
54+
putV4 d (V4 x y z t) = do
55+
let i8 = putComp d
5856
i8 x
5957
i8 y
6058
i8 z
6159
i8 t
6260

63-
instance v4IsDeserializable :: D.IsDeserializable V4 where
64-
get d = do
65-
let comp = D.get d
61+
getV4 d = do
62+
let comp = getComp d
6663
x <- comp
6764
y <- comp
6865
z <- comp
@@ -82,17 +79,15 @@ instance arbM4 :: Arbitrary M4 where
8279
arbitrary = M4 <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
8380

8481

85-
instance m4IsSerializable :: S.IsSerializable M4 where
86-
put d (M4 x y z t) = do
87-
let v4 = S.put d
82+
putM4 d (M4 x y z t) = do
83+
let v4 = putV4 d
8884
v4 x
8985
v4 y
9086
v4 z
9187
v4 t
9288

93-
instance m4IsDeserializable :: D.IsDeserializable M4 where
94-
get d = do
95-
let v4= D.get d
89+
getM4 d = do
90+
let v4= getV4 d
9691
x <- v4
9792
y <- v4
9893
z <- v4
@@ -145,13 +140,13 @@ main = do
145140
serdes :: M4 -> M4 -> M4 -> M4 -> Boolean
146141
serdes m0 m1 m2 m3 = forcePure $ do
147142
a <- S.serialized 256 $ \s -> do
148-
let p = S.put s
143+
let p = putM4 s
149144
p m0
150145
p m1
151146
p m2
152147
p m3
153148
d <- D.deserializer a
154-
let g = D.get d
149+
let g = getM4 d
155150
m0' <- g
156151
m1' <- g
157152
m2' <- g
@@ -161,10 +156,10 @@ serdes m0 m1 m2 m3 = forcePure $ do
161156
short :: M4 -> M4 -> Boolean
162157
short m0 m1 = forcePure $ do
163158
a <- S.serialized 256 $ \s -> do
164-
let p = S.put s
159+
let p = putM4 s
165160
p m0
166161
d <- D.deserializer a
167-
let g = D.get d
162+
let g = getM4 d
168163
m0' <- g
169164
m1' <- g
170165
return $ m0' == (Right m0) && m1' /= (Right m1) && m1' == (Left "Short read")

0 commit comments

Comments
 (0)