File: Types.hs

package info (click to toggle)
haskell-foundation 0.0.30-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 928 kB
  • sloc: haskell: 9,124; ansic: 570; makefile: 6
file content (50 lines) | stat: -rw-r--r-- 1,953 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- |
-- Module      : Foundation.Timing
-- License     : BSD-style
-- Maintainer  : Foundation maintainers
--
-- An implementation of a timing framework
--
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Foundation.Time.Types
    ( NanoSeconds(..)
    , Seconds(..)
    ) where

import           Data.Proxy
import           Basement.Imports
import           Basement.PrimType
import           Foundation.Numerical
import           Data.Coerce

-- | An amount of nanoseconds
newtype NanoSeconds = NanoSeconds Word64
    deriving (Show,Eq,Ord,Additive,Enum,Bounded)


instance PrimType NanoSeconds where
    type PrimSize NanoSeconds = 8
    primSizeInBytes _        = primSizeInBytes (Proxy :: Proxy Word64)
    primShiftToBytes _       = primShiftToBytes (Proxy :: Proxy Word64)
    primBaUIndex ba ofs      = primBaUIndex ba (coerce ofs)
    primMbaURead mba ofs     = primMbaURead mba (coerce ofs)
    primMbaUWrite mba ofs v  = primMbaUWrite mba (coerce ofs) (coerce v :: Word64)
    primAddrIndex addr ofs   = primAddrIndex addr (coerce ofs)
    primAddrRead addr ofs    = primAddrRead addr (coerce ofs)
    primAddrWrite addr ofs v = primAddrWrite addr (coerce ofs) (coerce v :: Word64)

-- | An amount of seconds
newtype Seconds = Seconds Word64
    deriving (Show,Eq,Ord,Additive,Enum,Bounded)

instance PrimType Seconds where
    type PrimSize Seconds    = 8
    primSizeInBytes _        = primSizeInBytes (Proxy :: Proxy Word64)
    primShiftToBytes _       = primShiftToBytes (Proxy :: Proxy Word64)
    primBaUIndex ba ofs      = primBaUIndex ba (coerce ofs)
    primMbaURead mba ofs     = primMbaURead mba (coerce ofs)
    primMbaUWrite mba ofs v  = primMbaUWrite mba (coerce ofs) (coerce v :: Word64)
    primAddrIndex addr ofs   = primAddrIndex addr (coerce ofs)
    primAddrRead addr ofs    = primAddrRead addr (coerce ofs)
    primAddrWrite addr ofs v = primAddrWrite addr (coerce ofs) (coerce v :: Word64)