si-bytes-0.1: Byte with type-level units (e.g. B, KB, MB, ...).
Safe HaskellNone
LanguageHaskell2010

Data.Bytes.Class.Conversion

Description

Provides typeclasses for convert between byte sizes.

Since: 0.1

Synopsis

High-level

class Conversion a where Source #

This class allows one to transform a bytes type to any Size. For types with existentially quantified Size (e.g. SomeSize, SomeNetSize), this will "undo" the existential quantification.

Since: 0.1

Associated Types

type Converted (t :: Size) a = (r :: Type) | r -> t Source #

Since: 0.1

Methods

convert_ :: forall (t :: Size). SingSize t => a -> Converted t a Source #

convert_ @_ @t x converts x to size t.

Examples

Expand
>>> let bytes = MkBytes 50_000 :: Bytes 'M Int
>>> let gBytes = convert_ @_ @G bytes
>>> :type gBytes
gBytes :: Bytes G Int
>>> gBytes
MkBytes 50
>>> let bytes = hideSize (MkBytes 0.2 :: Bytes 'T Float)
>>> let mBytes = convert_ @_ @M bytes
>>> :type mBytes
mBytes :: Bytes M Float
>>> mBytes
MkBytes 200000.0

Since: 0.1

Instances

Instances details
(FromInteger n, MGroup n) => Conversion (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

convert_ :: forall (t :: Size). SingSize t => SomeSize n -> Converted t (SomeSize n) Source #

(FromInteger n, MGroup n) => Conversion (SomeNet n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Methods

convert_ :: forall (t :: Size). SingSize t => SomeNet n -> Converted t (SomeNet n) Source #

(FromInteger n, MGroup n, SingSize s) => Conversion (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

convert_ :: forall (t :: Size). SingSize t => Bytes s n -> Converted t (Bytes s n) Source #

(FromInteger n, MGroup n, SingSize s) => Conversion (SomeNetDir s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Methods

convert_ :: forall (t :: Size). SingSize t => SomeNetDir s n -> Converted t (SomeNetDir s n) Source #

(FromInteger n, MGroup n) => Conversion (SomeNetSize d n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Methods

convert_ :: forall (t :: Size). SingSize t => SomeNetSize d n -> Converted t (SomeNetSize d n) Source #

(FromInteger n, MGroup n, SingSize s) => Conversion (NetBytes d s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Methods

convert_ :: forall (t :: Size). SingSize t => NetBytes d s n -> Converted t (NetBytes d s n) Source #

convert :: forall (t :: Size) -> SingSize t => forall a. Conversion a => a -> Converted t a Source #

Alternative to convert_ with -XTypeApplications, using -XRequiredTypeArguments.

Examples

Expand
>>> let bytes = MkBytes 50_000 :: Bytes 'M Int
>>> let gBytes = convert G bytes
>>> :type gBytes
gBytes :: Bytes G Int
>>> gBytes
MkBytes 50

Since: 0.1

Low-level

convertSize :: (FromInteger n, MGroup n) => Size -> Size -> n -> n Source #

Low level function for converting a numeric literal between byte sizes. convert b1 b2 converts from b1 to b2, e.g. convert G K = \n -> n * 1_000_000. The higher level byte types and functions should be preferred (e.g. Conversion, Data.Bytes.Class.Normalize), but this is here when it is needed.

Since: 0.1

convertWitness :: forall (s :: Size) n. (FromInteger n, MGroup n, SingSize s) => Size -> n -> n Source #

Low level function for converting a numeric literal from the inferred SingSize to the parameter Size. For instance,

>>> convertWitness @K M 5_000
5

This is slightly more principled than convertSize, but the higher level byte types and functions should still be preferred (e.g. Bytes, Normalize).

Since: 0.1