@hackage lazy-scope0.0.1

Alternative lazy ByteString and ST-like IO Handle

lazy-scope library appeared as an attempt to improve lazy IO API from bytestring package:

  • hGetContents closes handle which was open by somebody else.

  • hGetContents closes handle only on EOF

E.g. git-phoenix does GIT objects recovery. Recovered compressed file usually has trailing trash bytes after archive ends. In such circumstance bracket finalizer should check every handle before closing.

lazy-scope library provides hGetContents with alternative semantic - it never close the handle! Handle and values, derived from it, have a type parameter which prevents accidental thunk escape beyond open handle scope. Solution is based on ST monad.

import Lazy.Scope qualified as S
import Relude

main = do
  r <- S.withBinaryFile "/etc/hosts" ReadMode S.hGetContents
  S.unsnoc r `seq` return ()

Error:

  • Couldn't match type ‘s0’ with ‘s’
    Expected: S.Handle s -> S.LazyT s IO (S.Bs s0)
      Actual: S.Handle s -> S.LazyT s IO (S.Bs s)
      because type variable ‘s’ would escape its scope

Correct version:

import Data.ByteString.Lazy qualified as LBS
import Lazy.Scope qualified as S
import Relude

main = do
  r <- S.withBinaryFile "/etc/hosts" ReadMode (S.hGetContents >=> S.toLbs)
  LBS.unsnoc r `seq` return ()

The package has scoped alternatives for majority of Handle and ByteString functions from System.IO and Data.ByteString.Lazy modules correspondingly.

Development

Dev environment is provided by nix-shell

$ nix-shell
$ cabal test