fs-effectful
Safe HaskellNone
LanguageGHC2021

Effectful.FileSystem.HandleWriter.Static

Description

Provides a static effect for writing to a handle.

Since: 0.1

Synopsis

Effect

data HandleWriter (a :: Type -> Type) b Source #

Static effect for writing to a handle.

Since: 0.1

openBinaryFile :: forall (es :: [Effect]). (HandleWriter :> es, HasCallStack) => OsPath -> Bool -> Eff es (Handle 'HandleModeWrite) Source #

Lifted openBinaryFile.

Since: 0.1

withBinaryFile :: forall (es :: [Effect]) a. (HandleWriter :> es, HasCallStack) => OsPath -> Bool -> (Handle 'HandleModeWrite -> Eff es a) -> Eff es a Source #

Lifted withBinaryFile.

Since: 0.1

hClose :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Eff es () Source #

Lifted hClose.

Since: 0.1

hFlush :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Eff es () Source #

Lifted hFlush.

Since: 0.1

hSetFileSize :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Integer -> Eff es () Source #

Lifted hSetFileSize.

Since: 0.1

hSetBuffering :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> BufferMode -> Eff es () Source #

Lifted hSetBuffering.

Since: 0.1

hSeek :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> SeekMode -> Integer -> Eff es () Source #

Lifted hSeek.

Since: 0.1

hTell :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Eff es Integer Source #

Lifted hTell.

Since: 0.1

hSetEcho :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Bool -> Eff es () Source #

Lifted hSetEcho.

Since: 0.1

hPut :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> ByteString -> Eff es () Source #

Lifted hPut.

Since: 0.1

hPutNonBlocking :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> ByteString -> Eff es ByteString Source #

Lifted hPutNonBlocking.

Since: 0.1

hLockRaw :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Eff es () Source #

Attempts to exclusively lock a file, blocking or throwing an exception upon failure.

Since: 0.1

hTryLockRaw :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Eff es Bool Source #

Attempts to exclusively lock a file.

Since: 0.1

hUnlockRaw :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Eff es () Source #

Unlocks a locked file.

Since: 0.1

Handlers

runHandleWriter :: forall (es :: [Effect]) a. (HasCallStack, IOE :> es) => Eff (HandleWriter ': es) a -> Eff es a Source #

Runs HandleWriter in IO.

Since: 0.1

File handles

data Handle (p :: HandleMode) Source #

Wrapper for file handles that includes an index for the mode.

Since: 0.1

Instances

Instances details
HasField "unHandle" (Handle p :: Type) Handle Source #

Since: 0.1

Instance details

Defined in Effectful.FileSystem.Handle.Internal

Methods

getField :: Handle p -> Handle #

HasField "unLockedHandle" (LockedHandle p :: Type) (Handle p :: Type) Source #

Since: 0.1

Instance details

Defined in Effectful.FileSystem.Handle.Internal

Methods

getField :: LockedHandle p -> Handle p #

(k ~ A_Getter, a ~ Handle, b ~ Handle) => LabelOptic "unHandle" k (Handle p) (Handle p) a b Source #

Since: 0.1

Instance details

Defined in Effectful.FileSystem.Handle.Internal

Methods

labelOptic :: Optic k NoIx (Handle p) (Handle p) a b Source #

data HandleMode Source #

Possible handle modes.

Since: 0.1

Constructors

HandleModeWrite

Since: 0.1

type family CanWrite (hm :: HandleMode) where ... Source #

Since: 0.1

Equations

CanWrite 'HandleModeRead = Unsatisfiable ('Text "HandleModeRead does not have Write permission.") 
CanWrite 'HandleModeWrite = () 
CanWrite 'HandleModeReadWrite = () 

Locking

These functions bring some type-safety to file locking. Consider:

main :: (HandleWriter :> es) => Eff es ()
main = withBinaryFile path False $ \h -> do
  hLockRaw handle
  bs <- writeBytes @HandleModeWrite h
  hUnlockRaw handle
  print bs

writeBytes :: (CanWrite p, HandleWriter :> es) => Handle p -> Eff es ByteString
writeBytes handle = hPut handle "some bytes"

In this example, we could remove all locking logic from main and everything would still compile. On the other hand:

main :: (HandleWriter :> es) => Eff es ()
main = withBinaryFile path False $ \h -> withLockedFile h $ \lh -> do
  bs <- writeBytes @HandleModeWrite lh
  print bs

writeBytes :: (CanWrite p, HandleWriter :> es) => LockedHandle p -> Eff es ByteString
writeBytes lockedHandle = liftLocked (\h -> hPut h "some bytes") lockedHandle

Removing withLockedFile would cause a compilation error, since writeBytes requires a LockedHandle. The idea is to write most of the program's logic in terms of LockedHandle, using liftLocked to lift Handle functions.

data LockedHandle (p :: HandleMode) Source #

File handle with lock.

Since: 0.1

Instances

Instances details
HasField "unLockedHandle" (LockedHandle p :: Type) (Handle p :: Type) Source #

Since: 0.1

Instance details

Defined in Effectful.FileSystem.Handle.Internal

Methods

getField :: LockedHandle p -> Handle p #

(k ~ A_Getter, a ~ Handle p, b ~ Handle p) => LabelOptic "unLockedHandle" k (LockedHandle p) (LockedHandle p) a b Source #

Since: 0.1

Instance details

Defined in Effectful.FileSystem.Handle.Internal

liftLocked :: forall (p :: HandleMode) a. HasCallStack => (HasCallStack => Handle p -> a) -> LockedHandle p -> a Source #

Lifts a function on handle to one on locked handles.

Since: 0.1

withLockedFile Source #

Arguments

:: forall (p :: HandleMode) (es :: [Effect]) a. (CanWrite p, HasCallStack, HandleWriter :> es) 
=> Handle p

Handle to lock.

-> (LockedHandle p -> Eff es a)

Callback with locked handle.

-> Eff es a 

Runs a computation with an exclusively locked file.

Since: 0.1

withTryLockedFile Source #

Arguments

:: forall (p :: HandleMode) a (es :: [Effect]). (CanWrite p, HasCallStack, HandleWriter :> es) 
=> Handle p

Handle to lock.

-> (LockedHandle p -> Eff es a)

Handle callback.

-> Eff es (Maybe a) 

Like withSharedLockedFile, except the lock attempt does not block.

Since: 0.1

hLock Source #

Arguments

:: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HasCallStack, HandleWriter :> es) 
=> Handle p

Handle to lock.

-> Eff es (LockedHandle p) 

Like hLockRaw, but returns a LockedHandle.

Since: 0.1

hTryLock Source #

Arguments

:: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HasCallStack, HandleWriter :> es) 
=> Handle p

Handle to lock.

-> Eff es (Maybe (LockedHandle p)) 

Like hTryLockRaw, but returns a LockedHandle if it succeeds.

Since: 0.1

hUnlock Source #

Arguments

:: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HasCallStack, HandleWriter :> es) 
=> LockedHandle p

Handle to unlock.

-> Eff es (Handle p) 

Like hUnlockRaw, but returns the original handle.

Since: 0.1

Raw

withLockedFileRaw Source #

Arguments

:: forall (p :: HandleMode) (es :: [Effect]) a. (CanWrite p, HasCallStack, HandleWriter :> es) 
=> Handle p

Handle to lock.

-> Eff es a

Callback with locked handle.

-> Eff es a 

withLockedFile without LockedHandle.

Since: 0.1

withTryLockedFileRaw Source #

Arguments

:: forall (p :: HandleMode) a (es :: [Effect]). (CanWrite p, HasCallStack, HandleWriter :> es) 
=> Handle p

Handle to lock.

-> Eff es a

Handle callback.

-> Eff es (Maybe a) 

UTF-8 Utils

hPutUtf8 :: forall (p :: HandleMode) (es :: [Effect]). (CanWrite p, HandleWriter :> es, HasCallStack) => Handle p -> Text -> Eff es () Source #

hPut and encodeUtf8.

Since: 0.1

Misc

die :: forall (es :: [Effect]) a. (HandleWriter :> es, HasCallStack) => String -> Eff es a Source #

Write given error message to stderr and terminate with exitFailure.

Since: 0.1

Re-exports

data BufferMode #

Three kinds of buffering are supported: line-buffering, block-buffering or no-buffering. These modes have the following effects. For output, items are written out, or flushed, from the internal buffer according to the buffer mode:

  • line-buffering: the entire output buffer is flushed whenever a newline is output, the buffer overflows, a hFlush is issued, or the handle is closed.
  • block-buffering: the entire buffer is written out whenever it overflows, a hFlush is issued, or the handle is closed.
  • no-buffering: output is written immediately, and never stored in the buffer.

An implementation is free to flush the buffer more frequently, but not less frequently, than specified above. The output buffer is emptied as soon as it has been written out.

Similarly, input occurs according to the buffer mode for the handle:

  • line-buffering: when the buffer for the handle is not empty, the next item is obtained from the buffer; otherwise, when the buffer is empty, characters up to and including the next newline character are read into the buffer. No characters are available until the newline character is available or the buffer is full.
  • block-buffering: when the buffer for the handle becomes empty, the next block of data is read into the buffer.
  • no-buffering: the next input item is read and returned. The hLookAhead operation implies that even a no-buffered handle may require a one-character buffer.

The default buffering mode when a handle is opened is implementation-dependent and may depend on the file system object which is attached to that handle. For most implementations, physical files will normally be block-buffered and terminals will normally be line-buffered.

Constructors

NoBuffering

buffering is disabled if possible.

LineBuffering

line-buffering should be enabled if possible.

BlockBuffering (Maybe Int)

block-buffering should be enabled if possible. The size of the buffer is n items if the argument is Just n and is otherwise implementation-dependent.

Instances

Instances details
Read BufferMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Handle.Types

Show BufferMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Handle.Types

Eq BufferMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Handle.Types

Ord BufferMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Handle.Types

data ByteString #

A space-efficient representation of a Word8 vector, supporting many efficient operations.

A ByteString contains 8-bit bytes, or by using the operations from Data.ByteString.Char8 it can be interpreted as containing 8-bit characters.

Instances

Instances details
NFData ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Methods

rnf :: ByteString -> () #

Monoid ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Semigroup ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Data ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ByteString -> c ByteString #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c ByteString #

toConstr :: ByteString -> Constr #

dataTypeOf :: ByteString -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c ByteString) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ByteString) #

gmapT :: (forall b. Data b => b -> b) -> ByteString -> ByteString #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ByteString -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ByteString -> r #

gmapQ :: (forall d. Data d => d -> u) -> ByteString -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> ByteString -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> ByteString -> m ByteString #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ByteString -> m ByteString #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ByteString -> m ByteString #

IsString ByteString #

Beware: fromString truncates multi-byte characters to octets. e.g. "枯朶に烏のとまりけり秋の暮" becomes �6k�nh~�Q��n�

Instance details

Defined in Data.ByteString.Internal.Type

IsList ByteString #

Since: bytestring-0.10.12.0

Instance details

Defined in Data.ByteString.Internal.Type

Associated Types

type Item ByteString 
Instance details

Defined in Data.ByteString.Internal.Type

Read ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Show ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Eq ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Ord ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

Lift ByteString #

Since: bytestring-0.11.2.0

Instance details

Defined in Data.ByteString.Internal.Type

Methods

lift :: Quote m => ByteString -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => ByteString -> Code m ByteString #

type Item ByteString # 
Instance details

Defined in Data.ByteString.Internal.Type

data IOMode #

Instances

Instances details
Enum IOMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.IOMode

Ix IOMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.IOMode

Read IOMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.IOMode

Show IOMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.IOMode

Eq IOMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.IOMode

Methods

(==) :: IOMode -> IOMode -> Bool #

(/=) :: IOMode -> IOMode -> Bool #

Ord IOMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.IOMode

type OsPath = OsString #

Type representing filenames/pathnames.

This type doesn't add any guarantees over OsString.

data SeekMode #

A mode that determines the effect of hSeek hdl mode i.

Constructors

AbsoluteSeek

the position of hdl is set to i.

RelativeSeek

the position of hdl is set to offset i from the current position.

SeekFromEnd

the position of hdl is set to offset i from the end of the file.

Instances

Instances details
Enum SeekMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Device

Ix SeekMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Device

Read SeekMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Device

Show SeekMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Device

Eq SeekMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Device

Ord SeekMode #

Since: base-4.2.0.0

Instance details

Defined in GHC.Internal.IO.Device

data Text #

A space efficient, packed, unboxed Unicode text type.

Instances

Instances details
PrintfArg Text #

Since: text-1.2.2.0

Instance details

Defined in Data.Text

Binary Text #

Since: text-1.2.1.0

Instance details

Defined in Data.Text

Methods

put :: Text -> Put #

get :: Get Text #

putList :: [Text] -> Put #

NFData Text # 
Instance details

Defined in Data.Text

Methods

rnf :: Text -> () #

Monoid Text # 
Instance details

Defined in Data.Text

Methods

mempty :: Text #

mappend :: Text -> Text -> Text #

mconcat :: [Text] -> Text #

Semigroup Text #

Beware: stimes will crash if the given number does not fit into an Int.

Since: text-1.2.2.0

Instance details

Defined in Data.Text

Methods

(<>) :: Text -> Text -> Text #

sconcat :: NonEmpty Text -> Text #

stimes :: Integral b => b -> Text -> Text #

Data Text #

This instance preserves data abstraction at the cost of inefficiency. We omit reflection services for the sake of data abstraction.

This instance was created by copying the updated behavior of Data.Set.Set and Data.Map.Map. If you feel a mistake has been made, please feel free to submit improvements.

The original discussion is archived here: could we get a Data instance for Data.Text.Text?

The followup discussion that changed the behavior of Set and Map is archived here: Proposal: Allow gunfold for Data.Map, ...

Instance details

Defined in Data.Text

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Text -> c Text #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Text #

toConstr :: Text -> Constr #

dataTypeOf :: Text -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Text) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Text) #

gmapT :: (forall b. Data b => b -> b) -> Text -> Text #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Text -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Text -> r #

gmapQ :: (forall d. Data d => d -> u) -> Text -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Text -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Text -> m Text #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Text -> m Text #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Text -> m Text #

IsString Text #

Performs replacement on invalid scalar values:

>>> :set -XOverloadedStrings
>>> "\55555" :: Text
"\65533"
Instance details

Defined in Data.Text

Methods

fromString :: String -> Text #

IsList Text #

Performs replacement on invalid scalar values:

>>> :set -XOverloadedLists
>>> ['\55555'] :: Text
"\65533"

Since: text-1.2.0.0

Instance details

Defined in Data.Text

Associated Types

type Item Text 
Instance details

Defined in Data.Text

type Item Text = Char

Methods

fromList :: [Item Text] -> Text #

fromListN :: Int -> [Item Text] -> Text #

toList :: Text -> [Item Text] #

Read Text # 
Instance details

Defined in Data.Text

Show Text # 
Instance details

Defined in Data.Text.Show

Methods

showsPrec :: Int -> Text -> ShowS #

show :: Text -> String #

showList :: [Text] -> ShowS #

Eq Text # 
Instance details

Defined in Data.Text

Methods

(==) :: Text -> Text -> Bool #

(/=) :: Text -> Text -> Bool #

Ord Text # 
Instance details

Defined in Data.Text

Methods

compare :: Text -> Text -> Ordering #

(<) :: Text -> Text -> Bool #

(<=) :: Text -> Text -> Bool #

(>) :: Text -> Text -> Bool #

(>=) :: Text -> Text -> Bool #

max :: Text -> Text -> Text #

min :: Text -> Text -> Text #

Lift Text #

Since: text-1.2.4.0

Instance details

Defined in Data.Text

Methods

lift :: Quote m => Text -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => Text -> Code m Text #

type Item Text # 
Instance details

Defined in Data.Text

type Item Text = Char