Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/prelude/Binary/SizedBin.hs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


module NHC.Binary
  ( {-type-}  Size(..)
  , {-type-}  SizedBin(..)
  , sizedPut
  , sizedGetFAt
  ) where

import BinPtr     ({-type-} BinPtr, unsafeShiftBinPtr)
import Bin        ({-type-} Bin(..))
import BinHandle  ({-type-} BinHandle)
import CBinary    ({-class-}Binary(..))
import GetFAt     (getFAt)
import PutBits    (putBits)
import GetBits    (getBits)
import AlignBin   (alignBin)
import SizeofBin  (sizeofBin)
import EqualsBin  (equalsBin)
import CompareBin (compareBin)
import StdMem     (stdmem)

newtype Size = Size Int	deriving (Eq,Ord)
data SizedBin a = SB {sizeOf :: Size, bhOf :: BinHandle, binOf :: Bin a}

sizedPut    :: Binary a => BinHandle -> a -> IO (SizedBin a)
sizedGetFAt :: Binary a => SizedBin a -> a
 
-- Note: sizedCompress aligns the end of the object *after* taking
-- its size.  Hence, the size will reflect the true, *unpadded* number
-- of bits in the object.
sizedPut bh a = alignBin bh    >>
                put bh a       >>= \x-> 
                sizeofBin bh x >>= \n->
                alignBin bh    >>
                return (SB (Size n) bh x)
sizedGetFAt sb = getFAt (bhOf sb) (binOf sb)

-----------------------------------------
instance Eq (SizedBin a) where
  (SB (Size n) bh1 b) == (SB (Size m) bh2 c)       =  n==m && equalsBin n bh1 b bh2 c

instance Ord (SizedBin a) where
  compare (SB (Size n) bh1 b) (SB (Size m) bh2 c)  =  compareBin n m bh1 b bh2 c

-----------------------------------------

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.