Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eq and Ord instance for Builder #677

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Data/ByteString/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ import Prelude hiding (writeFile)

import Data.ByteString.Builder.Internal
import qualified Data.ByteString.Builder.Prim as P
import qualified Data.ByteString.Lazy.Internal as L
import Data.ByteString.Builder.ASCII
import Data.ByteString.Builder.RealFloat

Expand All @@ -265,14 +264,6 @@ import Foreign
import GHC.Base (unpackCString#, unpackCStringUtf8#,
unpackFoldrCString#, build)

-- | Execute a 'Builder' and return the generated chunks as a 'L.LazyByteString'.
-- The work is performed lazy, i.e., only when a chunk of the 'L.LazyByteString'
-- is forced.
{-# NOINLINE toLazyByteString #-} -- ensure code is shared
toLazyByteString :: Builder -> L.LazyByteString
toLazyByteString = toLazyByteStringWith
(safeStrategy L.smallChunkSize L.defaultChunkSize) L.Empty

{- Not yet stable enough.
See note on 'hPut' in Data.ByteString.Builder.Internal
-}
Expand Down
15 changes: 15 additions & 0 deletions Data/ByteString/Builder/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module Data.ByteString.Builder.Internal (
, lazyByteString

-- ** Execution
, toLazyByteString
, toLazyByteStringWith
, AllocationStrategy
, safeStrategy
Expand Down Expand Up @@ -414,6 +415,12 @@ instance Monoid Builder where
{-# INLINE mconcat #-}
mconcat = foldr mappend mempty

instance Eq Builder where
x == y = toLazyByteString x == toLazyByteString y

instance Ord Builder where
compare x y = compare (toLazyByteString x) (toLazyByteString y)

-- | Flush the current buffer. This introduces a chunk boundary.
{-# INLINE flush #-}
flush :: Builder
Expand Down Expand Up @@ -1040,6 +1047,14 @@ safeStrategy firstSize bufSize =
nextBuffer Nothing = newBuffer $ sanitize firstSize
nextBuffer (Just (_, minSize)) = newBuffer minSize

-- | Execute a 'Builder' and return the generated chunks as a 'L.LazyByteString'.
-- The work is performed lazy, i.e., only when a chunk of the 'L.LazyByteString'
-- is forced.
{-# NOINLINE toLazyByteString #-} -- ensure code is shared
toLazyByteString :: Builder -> L.LazyByteString
toLazyByteString = toLazyByteStringWith
(safeStrategy L.smallChunkSize L.defaultChunkSize) L.Empty

-- | /Heavy inlining./ Execute a 'Builder' with custom execution parameters.
--
-- This function is inlined despite its heavy code-size to allow fusing with
Expand Down