From ae42860d5dda255cba795652d9f67f7b2d2c2085 Mon Sep 17 00:00:00 2001 From: Fumiaki Kinoshita Date: Tue, 30 May 2023 17:15:27 +0900 Subject: [PATCH] add toStrictByteString to Data.ByteString.Builder --- Changelog.md | 1 + Data/ByteString/Builder.hs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/Changelog.md b/Changelog.md index 8d35d121c..a53630481 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ * [Remove `zipWith` rewrite rule](https://github.com/haskell/bytestring/pull/387) * [`ShortByteString` is now a wrapper over boxed `Data.Array.Byte.ByteArray` instead of unboxed `ByteArray#` directly](https://github.com/haskell/bytestring/pull/410) * [`fromListN` from `instance IsList ShortByteString` throws an exception if the first argument does not match the length of the second instead of silent ignore](https://github.com/haskell/bytestring/pull/410) +* [Add `toStrictByteString` to `Data.ByteString.Builder`](https://github.com/haskell/bytestring/pull/588) [0.12.0.0]: https://github.com/haskell/bytestring/compare/0.11.4.0...0.12.0.0 diff --git a/Data/ByteString/Builder.hs b/Data/ByteString/Builder.hs index a6a6a0dae..05513a368 100644 --- a/Data/ByteString/Builder.hs +++ b/Data/ByteString/Builder.hs @@ -189,6 +189,7 @@ module Data.ByteString.Builder -- cases. See "Data.ByteString.Builder.Extra", for information -- about fine-tuning them. , toLazyByteString + , toStrictByteString , hPutBuilder , writeFile @@ -260,6 +261,7 @@ import Prelude hiding (writeFile) import Data.ByteString.Builder.Internal import qualified Data.ByteString.Builder.Prim as P +import qualified Data.ByteString.Internal as S import qualified Data.ByteString.Lazy.Internal as L import Data.ByteString.Builder.ASCII import Data.ByteString.Builder.RealFloat @@ -278,6 +280,14 @@ toLazyByteString :: Builder -> L.ByteString toLazyByteString = toLazyByteStringWith (safeStrategy L.smallChunkSize L.defaultChunkSize) L.Empty +-- | Execute a 'Builder' and pack the resulting chunks into a strict 'S.ByteString'. +-- +-- @'toStrictByteString' = 'L.toStrict' . 'toLazyByteString'@ +-- +{-# INLINABLE toStrictByteString #-} +toStrictByteString :: Builder -> S.ByteString +toStrictByteString = L.toStrict . toLazyByteString + {- Not yet stable enough. See note on 'hPut' in Data.ByteString.Builder.Internal -}