Converting things to and from binary

Hal Daume III hdaume@ISI.EDU
Wed, 21 May 2003 09:15:45 -0700 (PDT)


Hi George,

> > I don't understand your example of the bit consumer and IORefs
> > though...could you explain it a bit more (no pun)?
> 
> See the message I just posted the libraries list for a better example.
>     http://haskell.org/pipermail/libraries/2003-May/001006.html

Why not use a state transformer on top of IO or something like that?

> Another one would be where you want to read some data from
> an untrusted client, limiting the number of bytes you can be bothered
> to read.  This could be done using my framework by extending the monad
> with a count, it can't be done in Malcolm's framework, at least not
> without introducing an extra first step to do the IO.

Hrm, this is interesting :).  It seems like you should be able to use
lazyGet to achieve this.  Opening a BinIO doesn't actually cause it to be
read until you actually do some of the reading.  Using lazyGet should
enable you to just read the amount you need to read under certain
circumstances.  I envision something like:

newtype UntrustedString = UntrustedString String

instance Binary UntrustedString where
  put_ bh (UntrustedString s) = put_ bh s  -- put_ is trusted
  get  bh = do
    s <- lazyGet bh
    let s' = deepSeq $ take 20 s
    s' `seq` closeHandle bh
    return (UntrustedString s')

or something like that (untested, of course).