lazy-scope: Alternative lazy ByteString and ST-like IO Handle

[ bsd3, library, system ] [ Propose Tags ] [ Report a vulnerability ]

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

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.1
Change log changelog.md
Dependencies base (>=4.7 && <5), bytestring (>=0.12.1 && <1), deepseq (<2), directory (<2), filepath (<2), mtl (<3), relude (>=1.2.2 && <2), trace-embrace (>=1.2.0 && <2), transformers (<1), unliftio (<1) [details]
Tested with ghc ==9.10.1 || ==9.12.2
License BSD-3-Clause
Copyright Daniil Iaitkov 2025
Author Daniil Iaitskov
Maintainer dyaitskov@gmail.com
Category System
Home page http://github.com/yaitskov/scoped-handle
Bug tracker https://github.com/yaitskov/lazy-scope/issues
Source repo head: git clone https://github.com/yaitskov/lazy-scope.git
Uploaded by DaniilIaitskov at 2025-07-15T00:23:36Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 3 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]