From ad79d55816d8aaad8e51cdab1268686fe598d42f Mon Sep 17 00:00:00 2001 From: Bodigrim Date: Sun, 10 May 2020 16:20:59 +0100 Subject: [PATCH] Add IsList instances --- Changelog.md | 1 + Data/ByteString/Internal.hs | 12 ++++++++++++ Data/ByteString/Lazy/Internal.hs | 14 +++++++++++++- Data/ByteString/Short/Internal.hs | 12 ++++++++++-- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 3a77d0f2d..249de059e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ 0.10.10.2 – * Add `takeWhileEnd`, `dropWhileEnd` and `strip` for strict bytestrings + * Add `IsList` instances 0.10.10.1 – June 2020 diff --git a/Data/ByteString/Internal.hs b/Data/ByteString/Internal.hs index 4f8050ba4..c39fb32a3 100644 --- a/Data/ByteString/Internal.hs +++ b/Data/ByteString/Internal.hs @@ -1,6 +1,7 @@ {-# LANGUAGE CPP, ForeignFunctionInterface, BangPatterns #-} {-# LANGUAGE UnliftedFFITypes, MagicHash, UnboxedTuples, DeriveDataTypeable #-} +{-# LANGUAGE TypeFamilies #-} #if __GLASGOW_HASKELL__ >= 703 {-# LANGUAGE Unsafe #-} #endif @@ -117,6 +118,10 @@ import Data.Data (Data(..), mkNoRepType) import GHC.Base (nullAddr#,realWorld#,unsafeChr) +#if MIN_VERSION_base(4,7,0) +import GHC.Exts (IsList(..)) +#endif + #if MIN_VERSION_base(4,4,0) import GHC.CString (unpackCString#) #else @@ -182,6 +187,13 @@ instance Show ByteString where instance Read ByteString where readsPrec p str = [ (packChars x, y) | (x, y) <- readsPrec p str ] +#if MIN_VERSION_base(4,7,0) +instance IsList ByteString where + type Item ByteString = Word8 + fromList = packBytes + toList = unpackBytes +#endif + instance IsString ByteString where fromString = packChars diff --git a/Data/ByteString/Lazy/Internal.hs b/Data/ByteString/Lazy/Internal.hs index bd07fb570..ec6d947eb 100644 --- a/Data/ByteString/Lazy/Internal.hs +++ b/Data/ByteString/Lazy/Internal.hs @@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE TypeFamilies #-} #if __GLASGOW_HASKELL__ >= 703 {-# LANGUAGE Unsafe #-} #endif @@ -13,7 +14,7 @@ -- Maintainer : dons00@gmail.com, duncan@community.haskell.org -- Stability : unstable -- Portability : non-portable --- +-- -- A module containing semi-public 'ByteString' internals. This exposes -- the 'ByteString' representation and low level construction functions. -- Modules which extend the 'ByteString' system will need to use this module @@ -68,6 +69,10 @@ import Data.String (IsString(..)) import Data.Typeable (Typeable) import Data.Data (Data(..), mkNoRepType) +#if MIN_VERSION_base(4,7,0) +import GHC.Exts (IsList(..)) +#endif + -- | A space-efficient representation of a 'Word8' vector, supporting many -- efficient operations. -- @@ -110,6 +115,13 @@ instance Show ByteString where instance Read ByteString where readsPrec p str = [ (packChars x, y) | (x, y) <- readsPrec p str ] +#if MIN_VERSION_base(4,7,0) +instance IsList ByteString where + type Item ByteString = Word8 + fromList = packBytes + toList = unpackBytes +#endif + instance IsString ByteString where fromString = packChars diff --git a/Data/ByteString/Short/Internal.hs b/Data/ByteString/Short/Internal.hs index 17ff47db0..5da0fbe21 100644 --- a/Data/ByteString/Short/Internal.hs +++ b/Data/ByteString/Short/Internal.hs @@ -1,6 +1,7 @@ {-# LANGUAGE DeriveDataTypeable, CPP, BangPatterns, RankNTypes, ForeignFunctionInterface, MagicHash, UnboxedTuples, UnliftedFFITypes #-} +{-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-} #if __GLASGOW_HASKELL__ >= 703 {-# LANGUAGE Unsafe #-} @@ -15,7 +16,7 @@ -- Maintainer : duncan@community.haskell.org -- Stability : stable -- Portability : ghc only --- +-- -- Internal representation of ShortByteString -- module Data.ByteString.Short.Internal ( @@ -170,6 +171,13 @@ instance Show ShortByteString where instance Read ShortByteString where readsPrec p str = [ (packChars x, y) | (x, y) <- readsPrec p str ] +#if MIN_VERSION_base(4,7,0) +instance GHC.Exts.IsList ShortByteString where + type Item ShortByteString = Word8 + fromList = packBytes + toList = unpackBytes +#endif + instance IsString ShortByteString where fromString = packChars @@ -198,7 +206,7 @@ length (SBS _ len) = len null :: ShortByteString -> Bool null sbs = length sbs == 0 --- | /O(1)/ 'ShortByteString' index (subscript) operator, starting from 0. +-- | /O(1)/ 'ShortByteString' index (subscript) operator, starting from 0. index :: ShortByteString -> Int -> Word8 index sbs i | i >= 0 && i < length sbs = unsafeIndex sbs i