streamly-bytestring: Library for streamly and bytestring interoperation.

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Please see the README on GitHub at https://github.com/psibi/streamly-bytestring#readme


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.1, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.2.0, 0.2.1, 0.2.1
Change log Changelog.md
Dependencies base (>=4.7 && <5), bytestring (>=0.11.0 && <0.11.3 || >=0.11.3.1 && <0.11.6 || >=0.12.0 && <0.12.1), streamly-core (>=0.1.0 && <0.2.2) [details]
License BSD-3-Clause
Copyright Sibi Prabakaran
Author Sibi Prabakaran
Maintainer sibi@psibi.in
Category Streamly, Stream, ByteString
Home page https://github.com/psibi/streamly-bytestring#readme
Bug tracker https://github.com/psibi/streamly-bytestring/issues
Source repo head: git clone https://github.com/psibi/streamly-bytestring
Uploaded by adithyaov at 2024-01-03T12:06:53Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for streamly-bytestring-0.2.1

[back to package description]

streamly-bytestring

Library for streamly and bytestring interoperation.

If you are writing code from scratch, please use Streamly.Data.Array which is a generalization of ByteString and better integrated with streamly.

This library is to enable interoperation of streamly with existing code that uses ByteString.

The package provides APIs to interconvert between strict Bytestring and streamly Array Word8 and between lazy Bytestring and stream of Array Word8.

The interconversion in the case of strict Bytestring and streamly Array Word8 has no overhead for GHC allocated memory. For foreign allocator allocated memory there is a copy involved.

Usage

This is a dumb program that counts the number of bytes in a file.

import qualified Streamly.Data.Stream as S
import qualified Streamly.Data.Fold as FL

import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL

import qualified Streamly.External.ByteString as Strict
import qualified Streamly.External.ByteString.Lazy as Lazy

import System.IO (FilePath)

strictByteStringSize :: BS.ByteString -> IO Int
strictByteStringSize bs = S.fold FL.length $ S.unfold Strict.reader bs

lazyByteStringSize :: BSL.ByteString -> IO Int
lazyByteStringSize bsl =
    S.fold (FL.foldl' (+) 0)
        $ S.mapM strictByteStringSize
        $ fmap Strict.fromArray
        $ Lazy.toChunks bsl

fileSize :: FilePath -> IO Int
fileSize path = do
    bsl <- BSL.readFile path
    lazyByteStringSize bsl