byte-order 0.1.1.0 → 0.1.2.0
raw patch · 4 files changed
+38/−3 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ System.ByteOrder.Class: instance System.ByteOrder.Class.Bytes GHC.Types.Word
Files
- CHANGELOG.md +4/−0
- byte-order.cabal +1/−1
- src/System/ByteOrder/Class.hs +18/−2
- test/Unit.hs +15/−0
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for byte-order +## 0.1.2.0 -- 2020-01-06++* Add a `Bytes` instance for `Word`.+ ## 0.1.1.0 -- YYYY-mm-dd * Add `PrimUnaligned` instance for `Fixed`.
byte-order.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: byte-order-version: 0.1.1.0+version: 0.1.2.0 synopsis: Portable big-endian and little-endian conversions description: This library provides an interface to portably work with byte
src/System/ByteOrder/Class.hs view
@@ -16,9 +16,11 @@ import Data.Int (Int8,Int16,Int32,Int64) import Data.Word (Word8,Word16,Word32,Word64) import Data.Word (byteSwap16,byteSwap32,byteSwap64)-import GHC.ByteOrder (ByteOrder(..),targetByteOrder)-import GHC.ByteOrder (ByteOrder(BigEndian,LittleEndian))+import GHC.ByteOrder (ByteOrder(BigEndian,LittleEndian),targetByteOrder)+import GHC.Word (Word(W#)) +import qualified GHC.Exts as Exts+ -- | Types that are represented as a fixed-sized word. For these -- types, the bytes can be swapped. The instances of this class -- use byteswapping primitives and compile-time knowledge of native@@ -65,6 +67,16 @@ BigEndian -> byteSwap64 LittleEndian -> id +instance Bytes Word where+ {-# inline toBigEndian #-}+ {-# inline toLittleEndian #-}+ toBigEndian = case targetByteOrder of+ BigEndian -> id+ LittleEndian -> byteSwap+ toLittleEndian = case targetByteOrder of+ BigEndian -> byteSwap+ LittleEndian -> id+ instance Bytes Int8 where {-# inline toBigEndian #-} {-# inline toLittleEndian #-}@@ -131,3 +143,7 @@ instance FixedOrdering 'BigEndian where toFixedEndian = toBigEndian++byteSwap :: Word -> Word+{-# inline byteSwap #-}+byteSwap (W# w) = W# (Exts.byteSwap# w)
test/Unit.hs view
@@ -17,6 +17,8 @@ testB putStrLn "C" testC+ putStrLn "D"+ testD putStrLn "Finished" testA :: IO ()@@ -58,6 +60,19 @@ expectByte name marr 5 0xAB expectByte name marr 6 0xCD expectByte name marr 7 0xEF++testD :: IO ()+testD = do+ let payload = 0x01234567 :: Word+ marr <- newByteArray 20+ setByteArray marr 0 20 (0x00 :: Word8)+ writeByteArray marr 0 (Fixed @'LittleEndian payload)+ let name = "testA"+ expectByte name marr 0 0x67+ expectByte name marr 1 0x45+ expectByte name marr 2 0x23+ expectByte name marr 3 0x01+ expectByte name marr 4 0x00 expectByte :: String -> MutableByteArray RealWorld -> Int -> Word8 -> IO () expectByte name marr ix w = do