knob-0.2.2: Memory-backed handles

Copyright | 2011 John Millikin
License | MIT
Maintainer | n@monade.li
Portability | GHC only
Safe Haskell | None
Language | Haskell2010

Data.Knob

Description

Create memory-backed `Handle`s, referencing virtual files. This is mostly useful for testing `Handle`-based APIs without having to interact with the filesystem.

    import Data.ByteString (pack)
    import Data.Knob
    import System.IO

    main = do
        knob <- newKnob (pack [])
        h <- newFileHandle knob "test.txt" WriteMode
        hPutStrLn h "Hello world!"
        hClose h
        bytes <- Data.Knob.getContents knob
        putStrLn ("Wrote bytes: " ++ show bytes)

Synopsis

- data Knob
- newKnob :: MonadIO m => ByteString -> m Knob
- getContents :: MonadIO m => Knob -> m ByteString
- setContents :: MonadIO m => Knob -> ByteString -> m ()
- newFileHandle :: MonadIO m => Knob -> String -> IOMode -> m Handle
- withFileHandle :: MonadIO m => Knob -> String -> IOMode -> (Handle -> IO a) -> m a
- data Device
- newDevice :: MonadIO m => Knob -> IOMode -> m Device

# Documentation

data Knob Source #

A knob is a basic virtual file, which contains a byte buffer. A knob can have multiple `Handle`s open to it, each of which behaves like a standard file handle.

Use `getContents` and `setContents` to inspect and modify the knob's byte buffer.

newKnob :: MonadIO m => ByteString -> m Knob Source #

getContents :: MonadIO m => Knob -> m ByteString Source #

setContents :: MonadIO m => Knob -> ByteString -> m () Source #

newFileHandle Source #

Arguments

:: MonadIO m |
=> Knob |
-> String | Filename shown in error messages
-> IOMode |
-> m Handle |

Create a new `Handle` pointing to a `Knob`. This handle behaves like a file-backed handle for most purposes.

withFileHandle Source #

Arguments

:: MonadIO m |
=> Knob |
-> String | Filename shown in error messages.
-> IOMode |
-> (Handle -> IO a) |
-> m a |

See `newFileHandle`.

data Device Source #

An IO device backed by a `Knob`. You shouldn't usually use this type directly; use `newFileHandle` or `withFileHandle` instead.

#### Instances

Instances details

BufferedIO Device Source # |

Instance details

Defined in Data.Knob

Methods

newBuffer :: Device -> BufferState -> IO (Buffer Word8) Source #

fillReadBuffer :: Device -> Buffer Word8 -> IO (Int, Buffer Word8) Source #

fillReadBuffer0 :: Device -> Buffer Word8 -> IO (Maybe Int, Buffer Word8) Source #

emptyWriteBuffer :: Device -> Buffer Word8 -> IO (Buffer Word8) Source #

flushWriteBuffer :: Device -> Buffer Word8 -> IO (Buffer Word8) Source #

flushWriteBuffer0 :: Device -> Buffer Word8 -> IO (Int, Buffer Word8) Source #

IODevice Device Source # |

Instance details

Defined in Data.Knob

Methods

ready :: Device -> Bool -> Int -> IO Bool Source #

close :: Device -> IO () Source #

isTerminal :: Device -> IO Bool Source #

isSeekable :: Device -> IO Bool Source #

seek :: Device -> SeekMode -> Integer -> IO Integer Source #

tell :: Device -> IO Integer Source #

getSize :: Device -> IO Integer Source #

setSize :: Device -> Integer -> IO () Source #

setEcho :: Device -> Bool -> IO () Source #

getEcho :: Device -> IO Bool Source #

setRaw :: Device -> Bool -> IO () Source #

devType :: Device -> IO IODeviceType Source #

dup :: Device -> IO Device Source #

dup2 :: Device -> Device -> IO Device Source #

RawIO Device Source # |
Instance details Defined in Data.Knob Methods read :: Device -> Ptr Word8 -> Word64 -> Int -> IO Int Source # readNonBlocking :: Device -> Ptr Word8 -> Word64 -> Int -> IO (Maybe Int) Source # write :: Device -> Ptr Word8 -> Word64 -> Int -> IO () Source # writeNonBlocking :: Device -> Ptr Word8 -> Word64 -> Int -> IO Int Source #

newDevice :: MonadIO m => Knob -> IOMode -> m Device Source #