MyNixOS website logo
Description

A simple and high performance IO toolkit for Haskell.

This package provides a simple and high performance IO toolkit for Haskell, including packed vectors, unicode texts, socket, file system, timers and more!

Haskell stdio: haskell standard input and output

Linux Build Status Windows Build Status

Welcome! Haskell stdio is a complete I/O toolkit powered by libuv, it features a multi-core io multiplexer and various improvements on packed data types. This project is still in infancy. Please join in!

    __  _____   _____ __ __ ________    __       _______________  ________ 
   / / / /   | / ___// //_// ____/ /   / /      / ___/_  __/ __ \/  _/ __ \
  / /_/ / /| | \__ \/ ,<  / __/ / /   / /       \__ \ / / / / / // // / / /
 / __  / ___ |___/ / /| |/ /___/ /___/ /___    ___/ // / / /_/ // // /_/ / 
/_/ /_/_/  |_/____/_/ |_/_____/_____/_____/   /____//_/ /_____/___/\____/

Install

On windows we have bundled libuv source, so no extra steps to be taken.

On *nix platforms, you should install libuv library first, you can use your distribution's package manager if available, for example:

# on debian/ubuntu, make sure to use 1.x
apt-get install libuv1-dev  libuv1

# on MacOS, we recommend brew
brew install libuv

...

Currently the minimum version requirement for libuv is v1.14. If your package manager's libuv doesn't meet this requirement, you can also build libuv from source following the guide here, e.g.

git clone https://github.com/libuv/libuv.git 
cd libuv 
git checkout tags/v1.24.0   # depend on your own need, any version >= 1.14 will work.
sh autogen.sh 
./configure 
make 
sudo make install 

After manually building and installing, you may need to modify your LIBRARY_PATH/CPATH if necessary. Now installing stdio is as easy as any other haskell packages.

cabal install stdio

Now you can fire GHCi and play around, or read the project overview, haddock.

Examples

  • hello world
import Std.IO.StdStream
import qualified Std.Data.Text as T

main = do
    -- read stdin and write to stdout, but with our new IO manager!
    input <- readLineStd
    printStd (T.validate input)
  • tcp echo server
import Std.IO.TCP
import Std.IO.Buffered
import Control.Monad

main = do
    startServer defaultServerConfig
        { serverAddr = SockAddrInet 8888 inetAny
        , serverWorker = echo
        }
  where
    echo uvs = forever $ do
        i <- newBufferedInput uvs 4096
        o <- newBufferedOutput uvs 4096
        readBuffer i >>= writeBuffer o
        flushBuffer o

Now try nc -v 127.0.0.1 8888.

  • logging
import Std.IO.Logger
import qualified Std.Data.Builder as B
import Control.Concurrent

main = withStdLogger $ do
    debug $ "hello world! PI ~=" >> B.double pi     -- debug level won't be immediately flushed
    forkIO $ do
        fatal "fatal message will trigger a log flush"
  • file system operatations
import           Std.IO.FileSystem
import           Std.IO.Resource
import           Std.IO.StdStream

main = do
    -- create a temp directory
    tempdir <- mkdtemp "temp"   
    let filename = "temp" <> "/test"
        flags = O_RDWR .|. O_CREAT      -- create if not exist
        mode = DEFAULT_MODE

    -- file is a 'Resource', use 'withResource' to automatically manage it
    withResource (initUVFile filename flags mode) $ \ f -> do
        o <- newBufferedOutput file 4096
        writeBuffer o "hello world!"
        flushBuffer o

    stat filename >>= printStd
Metadata

Version

0.2.0.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows