From e0762612e95b0816721165c5aba6189809fcde58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Tue, 27 Oct 2026 22:08:47 +0100 Subject: ByteString conversion classes --- src/Erebos/Invite.hs | 3 ++- src/Erebos/Object/Internal.hs | 9 +++++---- src/Erebos/PubKey.hs | 2 +- src/Erebos/Storage/Backend.hs | 4 ++-- src/Erebos/Storage/Internal.hs | 14 +++++++------- src/Erebos/Util.hs | 12 ++++++------ 6 files changed, 23 insertions(+), 21 deletions(-) (limited to 'src/Erebos') diff --git a/src/Erebos/Invite.hs b/src/Erebos/Invite.hs index 69a6977..fdf90dd 100644 --- a/src/Erebos/Invite.hs +++ b/src/Erebos/Invite.hs @@ -31,6 +31,7 @@ import Data.Text qualified as T import Data.Text.Encoding import Data.Time.LocalTime +import ConvertByteString import Crypto import Erebos.Contact import Erebos.Discovery @@ -79,7 +80,7 @@ instance Storable InviteData where newtype InviteToken = InviteToken ByteString - deriving (Eq, Ord, Semigroup, Monoid, ByteArray, ByteArrayAccess) + deriving (Eq, Ord, Semigroup, Monoid, ByteArray, ByteArrayAccess, ToByteString) showInviteToken :: InviteToken -> String showInviteToken (InviteToken token) = BC.unpack (showHex token) diff --git a/src/Erebos/Object/Internal.hs b/src/Erebos/Object/Internal.hs index 4a33dba..6f6fe6b 100644 --- a/src/Erebos/Object/Internal.hs +++ b/src/Erebos/Object/Internal.hs @@ -73,6 +73,7 @@ import Data.Word import System.IO.Unsafe +import ConvertByteString import Crypto import Erebos.Error import Erebos.Storage.Internal @@ -92,9 +93,9 @@ instance Eq (Ref' c) where instance Show (Ref' c) where show ref@(Ref st _) = show st ++ ":" ++ BC.unpack (showRef ref) -instance BA.ByteArrayAccess (Ref' c) where - length (Ref _ dgst) = BA.length dgst - withByteArray (Ref _ dgst) = BA.withByteArray dgst +instance ToByteString (Ref' c) where + withByteArray (Ref _ dgst) = withByteArray dgst + toByteString (Ref _ dgst) = toByteString dgst instance Hashable (Ref' c) where hashWithSalt salt = hashWithSalt salt . refDigest @@ -117,7 +118,7 @@ zeroRef s = Ref s (RefDigest h) digestAlgo = undefined isZeroRef :: Ref' c -> Bool -isZeroRef (Ref _ h) = all (==0) $ BA.unpack h +isZeroRef (Ref _ h) = all (== 0) $ B.unpack $ toByteString h refFromDigest :: Storage' c -> RefDigest -> IO (Maybe (Ref' c)) diff --git a/src/Erebos/PubKey.hs b/src/Erebos/PubKey.hs index ef56d41..02ec8be 100644 --- a/src/Erebos/PubKey.hs +++ b/src/Erebos/PubKey.hs @@ -12,9 +12,9 @@ module Erebos.PubKey ( import Control.Monad -import Data.ByteArray import Data.Text qualified as T +import ConvertByteString import Crypto import Erebos.Storable import Erebos.Storage.Key diff --git a/src/Erebos/Storage/Backend.hs b/src/Erebos/Storage/Backend.hs index 07bd63e..c802d95 100644 --- a/src/Erebos/Storage/Backend.hs +++ b/src/Erebos/Storage/Backend.hs @@ -18,10 +18,10 @@ module Erebos.Storage.Backend ( import Control.Concurrent.MVar -import Data.ByteArray qualified as BA import Data.ByteString (ByteString) import Data.HashTable.IO qualified as HT +import ConvertByteString import Erebos.Object.Internal import Erebos.Storage.Internal @@ -37,4 +37,4 @@ withStorageBackend Storage {..} f = f stBackend refDigestBytes :: RefDigest -> ByteString -refDigestBytes = BA.convert +refDigestBytes = toByteString diff --git a/src/Erebos/Storage/Internal.hs b/src/Erebos/Storage/Internal.hs index 49a2b27..5dbe9c2 100644 --- a/src/Erebos/Storage/Internal.hs +++ b/src/Erebos/Storage/Internal.hs @@ -25,8 +25,7 @@ import Control.DeepSeq import Control.Monad.Identity import Data.Bits -import Data.ByteArray (ByteArrayAccess, ScrubbedBytes) -import Data.ByteArray qualified as BA +import Data.ByteArray (ByteArrayAccess) import Data.ByteString (ByteString) import Data.ByteString.Char8 qualified as BC import Data.ByteString.Lazy qualified as BL @@ -40,6 +39,7 @@ import Foreign.Storable (peek) import System.IO.Unsafe (unsafePerformIO) import Crypto +import ConvertByteString import Erebos.UUID (UUID) import Erebos.Util @@ -168,13 +168,13 @@ watchListDel wid wl = wl { wlList = filter ((/= wid) . wlID) $ wlList wl } newtype RefDigest = RefDigest (Digest Blake2b_256) - deriving (Eq, Ord, NFData, ByteArrayAccess) + deriving (Eq, Ord, NFData, ByteArrayAccess, ToByteString) instance Show RefDigest where show = BC.unpack . showRefDigest instance Hashable RefDigest where - hashWithSalt salt ref = salt `xor` unsafePerformIO (BA.withByteArray ref peek) + hashWithSalt salt ref = salt `xor` unsafePerformIO (withByteArray ref peek) showRefDigestParts :: RefDigest -> (ByteString, ByteString) showRefDigestParts x = (BC.pack "blake2", showHex x) @@ -184,9 +184,9 @@ showRefDigest = showRefDigestParts >>> \(alg, hex) -> alg <> BC.pack "#" <> hex readRefDigest :: ByteString -> Maybe RefDigest readRefDigest x = case BC.split '#' x of - [alg, dgst] | BA.convert alg == BC.pack "blake2" -> - refDigestFromByteString =<< readHex dgst - _ -> Nothing + [ alg, dgst ] | alg == BC.pack "blake2" -> + refDigestFromByteString =<< readHex dgst + _ -> Nothing refDigestFromByteString :: ByteString -> Maybe RefDigest refDigestFromByteString = fmap RefDigest . digestFromByteString diff --git a/src/Erebos/Util.hs b/src/Erebos/Util.hs index 0d53e98..1c14417 100644 --- a/src/Erebos/Util.hs +++ b/src/Erebos/Util.hs @@ -2,12 +2,12 @@ module Erebos.Util where import Control.Monad -import Data.ByteArray (ByteArray, ByteArrayAccess) -import Data.ByteArray qualified as BA import Data.ByteString (ByteString) import Data.ByteString qualified as B import Data.Char +import ConvertByteString + uniq :: Eq a => [a] -> [a] uniq (x:y:xs) | x == y = uniq (x:xs) @@ -47,15 +47,15 @@ intersectsSorted (x:xs) (y:ys) | x < y = intersectsSorted xs (y:ys) intersectsSorted _ _ = False -showHex :: ByteArrayAccess ba => ba -> ByteString -showHex = B.concat . map showHexByte . BA.unpack +showHex :: ToByteString ba => ba -> ByteString +showHex = B.concat . map showHexByte . B.unpack . toByteString where showHexChar x | x < 10 = x + o '0' | otherwise = x + o 'a' - 10 showHexByte x = B.pack [ showHexChar (x `div` 16), showHexChar (x `mod` 16) ] o = fromIntegral . ord -readHex :: ByteArray ba => ByteString -> Maybe ba -readHex = return . BA.concat <=< readHex' +readHex :: FromByteString ba => ByteString -> Maybe ba +readHex = return . fromByteString . B.concat <=< readHex' where readHex' bs | B.null bs = Just [] readHex' bs = do (bx, bs') <- B.uncons bs (by, bs'') <- B.uncons bs' -- cgit v1.2.3