Skip to content

Commit

Permalink
Add safeEncode, safeEncodeLazy, safeDecode, safeDecodeLazy. These are
Browse files Browse the repository at this point in the history
safecopy equivalents of functions from Data.Serialize.
  • Loading branch information
ddssff committed Jul 20, 2023
1 parent d4d0d54 commit 85ba2d0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Data/SafeCopy/SafeCopy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import qualified Control.Monad.Trans.State as State (get)
import Control.Monad.Trans.RWS as RWS (evalRWST, modify, RWST, tell)
import qualified Control.Monad.Trans.RWS as RWS (get)
import Data.Bits (shiftR)
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.ByteString.Char8 as B
import Data.Int (Int32)
import Data.List
import Data.Map as Map (Map, lookup, insert)
Expand Down Expand Up @@ -575,6 +577,27 @@ isObviouslyConsistent Primitive = True
isObviouslyConsistent Base = True
isObviouslyConsistent _ = False

-------------------------------------------------
-- Some SafeCopy versions of Serialize functions.

-- | Encode a value using binary serialization to a strict ByteString.
safeEncode :: SafeCopy a => a -> B.ByteString
safeEncode = runPut . safePut

-- | Encode a value using binary serialization to a lazy ByteString.
safeEncodeLazy :: SafeCopy a => a -> L.ByteString
safeEncodeLazy = runPutLazy . safePut

-- | Decode a value from a strict ByteString, reconstructing the original
-- structure.
safeDecode :: SafeCopy a => B.ByteString -> Either String a
safeDecode = runGet safeGet

-- | Decode a value from a lazy ByteString, reconstructing the original
-- structure.
safeDecodeLazy :: SafeCopy a => L.ByteString -> Either String a
safeDecodeLazy = runGetLazy safeGet

-------------------------------------------------
-- Small utility functions that mean we don't
-- have to depend on ScopedTypeVariables.
Expand Down

0 comments on commit 85ba2d0

Please sign in to comment.