{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module System.Mem.Disk.Bytes
( DiskBytes
, toDiskBytes
, fromDiskBytes
) where
import Data.ByteString
( ByteString )
import Data.Int
( Int64 )
import System.IO.Unsafe
( unsafePerformIO )
import System.Mem.Disk.DiskApi
( Disk (..) )
import GHC.Exts (Int#, Int(I#))
import GHC.Base
import GHC.Weak
data DiskBytes = DiskBytes
{ DiskBytes -> Int#
index :: Int#
, DiskBytes -> Disk
disk :: Disk
}
addFinalizerBytes :: DiskBytes -> IO () -> IO ()
addFinalizerBytes :: DiskBytes -> IO () -> IO ()
addFinalizerBytes v :: DiskBytes
v@(DiskBytes{}) (IO State# RealWorld -> (# State# RealWorld, () #)
finalizer) = forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case mkWeak# :: forall a b c.
a
-> b
-> (State# RealWorld -> (# State# RealWorld, c #))
-> State# RealWorld
-> (# State# RealWorld, Weak# b #)
mkWeak# DiskBytes
v DiskBytes
v State# RealWorld -> (# State# RealWorld, () #)
finalizer State# RealWorld
s of (# State# RealWorld
s1, Weak# DiskBytes
w #) -> (# State# RealWorld
s1, () #)
toDiskBytes :: Disk -> ByteString -> DiskBytes
toDiskBytes :: Disk -> ByteString -> DiskBytes
toDiskBytes Disk
disk = forall a. IO a -> a
unsafePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Disk -> ByteString -> IO DiskBytes
mkDiskBytes Disk
disk
mkDiskBytes :: Disk -> ByteString -> IO DiskBytes
mkDiskBytes :: Disk -> ByteString -> IO DiskBytes
mkDiskBytes Disk
disk ByteString
bytes = do
I# Int#
index <- forall a. Enum a => a -> Int
fromEnum forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Disk -> ByteString -> IO Int64
put Disk
disk ByteString
bytes
let diskbytes :: DiskBytes
diskbytes = DiskBytes{Int#
index :: Int#
index :: Int#
index,Disk
disk :: Disk
disk :: Disk
disk}
DiskBytes
diskbytes seq :: forall a b. a -> b -> b
`seq` (DiskBytes -> IO () -> IO ()
addFinalizerBytes DiskBytes
diskbytes forall a b. (a -> b) -> a -> b
$ DiskBytes -> IO ()
finalizerBytes DiskBytes
diskbytes)
forall (f :: * -> *) a. Applicative f => a -> f a
pure DiskBytes
diskbytes
{-# NOINLINE finalizerBytes #-}
finalizerBytes :: DiskBytes -> IO ()
finalizerBytes :: DiskBytes -> IO ()
finalizerBytes DiskBytes{Int#
index :: Int#
index :: DiskBytes -> Int#
index,Disk
disk :: Disk
disk :: DiskBytes -> Disk
disk} =
Disk -> Int64 -> IO ()
delete Disk
disk forall a b. (a -> b) -> a -> b
$ forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ Int# -> Int
I# Int#
index
fromDiskBytes :: DiskBytes -> ByteString
fromDiskBytes :: DiskBytes -> ByteString
fromDiskBytes = forall a. IO a -> a
unsafePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiskBytes -> IO ByteString
unpackM
unpackM :: DiskBytes -> IO ByteString
unpackM :: DiskBytes -> IO ByteString
unpackM DiskBytes{Int#
index :: Int#
index :: DiskBytes -> Int#
index,Disk
disk :: Disk
disk :: DiskBytes -> Disk
disk} =
Disk -> Int64 -> IO ByteString
get Disk
disk forall a b. (a -> b) -> a -> b
$ forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ Int# -> Int
I# Int#
index