Skip to content

Commit 012bcb5

Browse files
committed
Support GHC-9.4, add ByteArray instance
1 parent 22c9f11 commit 012bcb5

File tree

5 files changed

+111
-21
lines changed

5 files changed

+111
-21
lines changed

.github/workflows/haskell-ci.yml

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#
99
# For more information, see https://github.com/haskell-CI/haskell-ci
1010
#
11-
# version: 0.13.20211030
11+
# version: 0.15.20220808
1212
#
13-
# REGENDATA ("0.13.20211030",["github","cabal.project"])
13+
# REGENDATA ("0.15.20220808",["github","cabal.project"])
1414
#
1515
name: Haskell-CI
1616
on:
@@ -23,22 +23,29 @@ on:
2323
jobs:
2424
linux:
2525
name: Haskell-CI - Linux - ${{ matrix.compiler }}
26-
runs-on: ubuntu-18.04
26+
runs-on: ubuntu-20.04
27+
timeout-minutes:
28+
60
2729
container:
2830
image: buildpack-deps:bionic
2931
continue-on-error: ${{ matrix.allow-failure }}
3032
strategy:
3133
matrix:
3234
include:
33-
- compiler: ghc-9.2.1
35+
- compiler: ghc-9.4.1
3436
compilerKind: ghc
35-
compilerVersion: 9.2.1
37+
compilerVersion: 9.4.1
3638
setup-method: ghcup
3739
allow-failure: false
38-
- compiler: ghc-9.0.1
40+
- compiler: ghc-9.2.4
3941
compilerKind: ghc
40-
compilerVersion: 9.0.1
41-
setup-method: hvr-ppa
42+
compilerVersion: 9.2.4
43+
setup-method: ghcup
44+
allow-failure: false
45+
- compiler: ghc-9.0.2
46+
compilerKind: ghc
47+
compilerVersion: 9.0.2
48+
setup-method: ghcup
4249
allow-failure: false
4350
- compiler: ghc-8.10.4
4451
compilerKind: ghc
@@ -98,18 +105,18 @@ jobs:
98105
apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo5
99106
if [ "${{ matrix.setup-method }}" = ghcup ]; then
100107
mkdir -p "$HOME/.ghcup/bin"
101-
curl -sL https://downloads.haskell.org/ghcup/0.1.17.3/x86_64-linux-ghcup-0.1.17.3 > "$HOME/.ghcup/bin/ghcup"
108+
curl -sL https://downloads.haskell.org/ghcup/0.1.18.0/x86_64-linux-ghcup-0.1.18.0 > "$HOME/.ghcup/bin/ghcup"
102109
chmod a+x "$HOME/.ghcup/bin/ghcup"
103-
"$HOME/.ghcup/bin/ghcup" install ghc "$HCVER"
104-
"$HOME/.ghcup/bin/ghcup" install cabal 3.6.2.0
110+
"$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false)
111+
"$HOME/.ghcup/bin/ghcup" install cabal 3.6.2.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
105112
else
106113
apt-add-repository -y 'ppa:hvr/ghc'
107114
apt-get update
108115
apt-get install -y "$HCNAME"
109116
mkdir -p "$HOME/.ghcup/bin"
110-
curl -sL https://downloads.haskell.org/ghcup/0.1.17.3/x86_64-linux-ghcup-0.1.17.3 > "$HOME/.ghcup/bin/ghcup"
117+
curl -sL https://downloads.haskell.org/ghcup/0.1.18.0/x86_64-linux-ghcup-0.1.18.0 > "$HOME/.ghcup/bin/ghcup"
111118
chmod a+x "$HOME/.ghcup/bin/ghcup"
112-
"$HOME/.ghcup/bin/ghcup" install cabal 3.6.2.0
119+
"$HOME/.ghcup/bin/ghcup" install cabal 3.6.2.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
113120
fi
114121
env:
115122
HCKIND: ${{ matrix.compilerKind }}
@@ -169,6 +176,10 @@ jobs:
169176
repository hackage.haskell.org
170177
url: http://hackage.haskell.org/
171178
EOF
179+
cat >> $CABAL_CONFIG <<EOF
180+
program-default-options
181+
ghc-options: $GHCJOBS +RTS -M3G -RTS
182+
EOF
172183
cat $CABAL_CONFIG
173184
- name: versions
174185
run: |
@@ -248,7 +259,7 @@ jobs:
248259
${CABAL} -vnormal check
249260
- name: haddock
250261
run: |
251-
$CABAL v2-haddock $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all
262+
$CABAL v2-haddock --haddock-all $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all
252263
- name: unconstrained build
253264
run: |
254265
rm -f cabal.project.local

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.3
2+
3+
- Add `ByteArray` (from `Data.Array.Byte` instance)
4+
15
# 1.0.2
26

37
- Add `Solo` instance

binary-orphans.cabal

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cabal-version: 1.12
22
name: binary-orphans
3-
version: 1.0.2
4-
x-revision: 1
3+
version: 1.0.3
54
synopsis: Compatibility package for binary; provides instances
65
category: Data, Binary, Parsing, Compatibility
76
description:
@@ -26,8 +25,9 @@ tested-with:
2625
|| ==8.6.5
2726
|| ==8.8.4
2827
|| ==8.10.4
29-
|| ==9.0.1
30-
|| ==9.2.1
28+
|| ==9.0.2
29+
|| ==9.2.4
30+
|| ==9.4.1
3131

3232
extra-source-files: CHANGELOG.md
3333

@@ -38,10 +38,11 @@ source-repository head
3838
library
3939
default-language: Haskell2010
4040
hs-source-dirs: src
41+
ghc-options: -Wall
4142
exposed-modules: Data.Binary.Orphans
4243
other-extensions: CPP
4344
build-depends:
44-
base >=4.5 && <4.17
45+
base >=4.5 && <4.18
4546
, binary >=0.5.1.0 && <0.6 || >=0.7.1.0 && <0.8 || >=0.8.3.0 && <0.8.10
4647
, transformers >=0.3.0.0 && <0.7
4748

@@ -68,7 +69,7 @@ test-suite binary-orphans-test
6869
, binary-orphans
6970
, OneTuple >=0.3 && <0.4
7071
, QuickCheck >=2.13.1 && <2.15
71-
, quickcheck-instances >=0.3.21 && <0.4
72+
, quickcheck-instances >=0.3.28 && <0.4
7273
, tagged >=0.8.6 && <0.8.7
7374
, tasty >=0.10.1.2 && <1.5
7475
, tasty-quickcheck >=0.8.3.2 && <0.11

src/Data/Binary/Orphans.hs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE FlexibleContexts #-}
33
{-# LANGUAGE FlexibleInstances #-}
4+
{-# LANGUAGE MagicHash #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE UnboxedTuples #-}
57
#if __GLASGOW_HASKELL__ >= 706
68
{-# LANGUAGE PolyKinds #-}
79
#endif
@@ -12,7 +14,8 @@ import Data.Binary
1214
import Data.Binary.Put
1315

1416
import Control.Applicative (Alternative (..))
15-
import Control.Monad (MonadPlus (..), liftM, liftM2)
17+
import Control.Monad
18+
(MonadPlus (..), liftM, liftM2, replicateM)
1619
import qualified Control.Monad.Fail as Fail
1720
import Data.Bits (Bits, shiftL, shiftR, (.|.))
1821
import Data.Complex (Complex (..))
@@ -34,6 +37,15 @@ import Data.Tuple (Solo (..))
3437
import Data.Tuple.Solo (Solo (..))
3538
#endif
3639

40+
#if MIN_VERSION_base(4,17,0)
41+
import Data.Array.Byte (ByteArray (..), MutableByteArray (..))
42+
import GHC.Exts
43+
(Int (..), indexWord8Array#, newByteArray#, sizeofByteArray#,
44+
unsafeFreezeByteArray#, writeWord8Array#)
45+
import GHC.ST (ST (..), runST)
46+
import GHC.Word (Word8 (..))
47+
#endif
48+
3749
-------------------------------------------------------------------------------
3850
-- binary-0.7.1.0
3951
-------------------------------------------------------------------------------
@@ -337,3 +349,58 @@ instance Binary a => Binary (Identity a) where
337349
instance Binary a => Binary (Solo a) where
338350
put (Solo x) = put x
339351
get = fmap Solo get
352+
353+
#if MIN_VERSION_base(4,17,0)
354+
instance Binary ByteArray where
355+
put ba = put maxI >> go 0
356+
where
357+
maxI :: Int
358+
maxI = sizeofByteArray ba
359+
360+
go :: Int -> Put
361+
go i | i < maxI = put (indexByteArray ba i) >> go (i + 1)
362+
| otherwise = return ()
363+
364+
get = do
365+
len <- get
366+
xs <- replicateM len get
367+
return (byteArrayFromListN len xs)
368+
369+
{-# INLINE sizeofByteArray #-}
370+
sizeofByteArray :: ByteArray -> Int
371+
sizeofByteArray (ByteArray ba) = I# (sizeofByteArray# ba)
372+
373+
{-# INLINE indexByteArray #-}
374+
indexByteArray :: ByteArray -> Int -> Word8
375+
indexByteArray (ByteArray ba) (I# i) = W8# (indexWord8Array# ba i)
376+
377+
{-# INLINE byteArrayFromListN #-}
378+
byteArrayFromListN :: Int -> [Word8] -> ByteArray
379+
byteArrayFromListN len xs = runST $ do
380+
mba <- newByteArray len
381+
go mba 0 xs
382+
unsafeFreezeByteArray mba
383+
where
384+
go :: MutableByteArray s -> Int -> [Word8] -> ST s ()
385+
go mba i ys
386+
| i < len = case ys of
387+
[] -> writeWord8Array mba i 0 >> go mba (i + 1) ys
388+
z:zs -> writeWord8Array mba i z >> go mba (i + 1) zs
389+
390+
| otherwise = return ()
391+
392+
{-# INLINE newByteArray #-}
393+
newByteArray :: Int -> ST s (MutableByteArray s)
394+
newByteArray (I# len) = ST $ \s -> case newByteArray# len s of
395+
(# s', mba #) -> (# s', MutableByteArray mba #)
396+
397+
{-# INLINE unsafeFreezeByteArray #-}
398+
unsafeFreezeByteArray :: MutableByteArray s -> ST s ByteArray
399+
unsafeFreezeByteArray (MutableByteArray mba) = ST $ \s -> case unsafeFreezeByteArray# mba s of
400+
(# s', ba #) -> (# s', ByteArray ba #)
401+
402+
{-# INLINE writeWord8Array #-}
403+
writeWord8Array :: MutableByteArray s -> Int -> Word8 -> ST s ()
404+
writeWord8Array (MutableByteArray mba) (I# i) (W8# w) = ST $ \s -> case writeWord8Array# mba i w s of
405+
s' -> (# s', () #)
406+
#endif

test/Tests.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import Test.QuickCheck.Instances ()
1313
import Test.Tasty (TestTree, defaultMain, testGroup)
1414
import Test.Tasty.QuickCheck (testProperty)
1515

16+
#if MIN_VERSION_base(4,17,0)
17+
import Data.Array.Byte (ByteArray)
18+
#endif
19+
1620
main :: IO ()
1721
main = defaultMain tests
1822

@@ -22,6 +26,9 @@ tests = testGroup "Roundtrip"
2226
, testProperty "Sum Int" $ roundtrip (Proxy :: Proxy (Sum Int))
2327
, testProperty "Min Int" $ roundtrip (Proxy :: Proxy (Min Int))
2428
, testProperty "Solo Int" $ roundtrip (Proxy :: Proxy (Solo Int))
29+
#if MIN_VERSION_base(4,17,0)
30+
, testProperty "ByteArray" $ roundtrip (Proxy :: Proxy ByteArray)
31+
#endif
2532
]
2633

2734
roundtrip :: (Eq a, Show a, Binary a) => Proxy a -> a -> Property

0 commit comments

Comments
 (0)