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

NFData instances for FsPath, HasFS and Handle #70

Merged
merged 1 commit into from
Jun 3, 2024
Merged
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
1 change: 1 addition & 0 deletions fs-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Add compound functions, built from the new primitives in `HasFS`:
`hGetBufExactly`, `hGetBufExactlyAt`, `hPutBufExactly`, and
`hPutBufExactlyAt`.
* `NFData` instances for `FsPath`, `HasFS` and `Handle`.

## 0.2.0.1 -- 2023-10-30

Expand Down
17 changes: 9 additions & 8 deletions fs-api/fs-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ library

default-language: Haskell2010
build-depends:
, base >=4.14 && <4.20
, bytestring >=0.10 && <0.13
, containers >=0.5 && <0.7
, base >=4.14 && <4.20
, bytestring >=0.10 && <0.13
, containers >=0.5 && <0.7
, deepseq
, digest
, directory >=1.3 && <1.4
, filepath >=1.4 && <1.5
, io-classes >=0.3 && <1.5
, primitive ^>=0.9
, text >=1.2 && <2.2
, directory >=1.3 && <1.4
, filepath >=1.4 && <1.5
, io-classes >=0.3 && <1.5
, primitive ^>=0.9
, safe-wild-cards ^>=1.0
, text >=1.2 && <2.2

if os(windows)
hs-source-dirs: src-win32
Expand Down
22 changes: 22 additions & 0 deletions fs-api/src/System/FS/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}

-- | An abstract view over the filesystem.
module System.FS.API (
Expand All @@ -27,13 +28,15 @@ module System.FS.API (
, hPutBufExactlyAt
) where

import Control.DeepSeq (NFData (..))
import Control.Monad.Class.MonadThrow
import Control.Monad.Primitive (PrimMonad (..))
import qualified Data.ByteString as BS
import Data.Int (Int64)
import Data.Primitive (MutableByteArray)
import Data.Set (Set)
import Data.Word
import SafeWildCards
import System.Posix.Types (ByteCount)

import System.FS.API.Types as Types
Expand Down Expand Up @@ -347,3 +350,22 @@ hPutBufExactlyAt hbfs h buf bufOff c off = go c off bufOff
else go remainingCount'
(currentOffset + fromIntegral writtenBytes)
(currentBufOff + fromIntegral writtenBytes)

{-------------------------------------------------------------------------------
Other
--------------------------------------------------------------------------------}

-- Without this, the module won't compile because the instance below is in the
-- same declaration group as the datatype definition. For more info, see
-- https://blog.monadfix.com/th-groups.
$(pure[])

instance NFData (HasFS m h) where
rnf $(fields 'HasFS) =
dumpState `seq` hOpen `seq` hClose `seq` hIsOpen `seq` hSeek `seq`
hGetSome `seq`hGetSomeAt `seq` hPutSome `seq` hTruncate `seq`
hGetSize `seq` createDirectory `seq` createDirectoryIfMissing `seq`
listDirectory `seq` doesDirectoryExist `seq` doesFileExist `seq`
removeDirectoryRecursive `seq` removeFile `seq` renameFile `seq`
mkFsErrorPath `seq` unsafeToFilePath `seq` hGetBufSome `seq`
hGetBufSomeAt `seq` hPutBufSome `seq` hPutBufSomeAt `seq` ()
8 changes: 5 additions & 3 deletions fs-api/src/System/FS/API/Types.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}

-- For Show Errno and Condense SeekMode instances
{-# OPTIONS_GHC -Wno-orphans #-}
Expand Down Expand Up @@ -45,7 +43,7 @@ module System.FS.API.Types (
, ioToFsErrorType
) where

import Control.DeepSeq (force)
import Control.DeepSeq (NFData (..), force)
import Control.Exception
import Data.Function (on)
import Data.List (intercalate, stripPrefix)
Expand Down Expand Up @@ -100,6 +98,7 @@ allowExisting openMode = case openMode of
-- | A relative path.
newtype FsPath = UnsafeFsPath { fsPathToList :: [Strict.Text] }
deriving (Eq, Ord, Generic)
deriving newtype NFData

fsPathFromList :: [Strict.Text] -> FsPath
fsPathFromList = UnsafeFsPath . force
Expand Down Expand Up @@ -184,6 +183,9 @@ data Handle h = Handle {
}
deriving (Generic)

instance NFData h => NFData (Handle h) where
rnf (Handle handleRaw handlePath) = rnf handleRaw `seq` rnf handlePath

instance Eq h => Eq (Handle h) where
(==) = (==) `on` handleRaw

Expand Down