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

Data.Bytes.Internal

Description

Internal module for Data.Bytes. The primary difference is this module exposes some underlying details i.e. singleton witnesses. These are hidden by default as they complicate the API.

Since: 0.1

Synopsis

Bytes

newtype Bytes (s :: Size) n Source #

This is the core type for handling type-safe byte operations. It is intended to be used as a simple wrapper over some numeric type, equipped with a Size tag.

To take full advantage of the API (e.g. normalize), the underlying numeric type should implement Semifield or, ideally, Field.

Examples

Expand
>>> MkBytes @M 1000
MkBytes 1000

Since: 0.1

Constructors

MkBytes n 

Instances

Instances details
HasField "unBytes" (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

getField :: Bytes s n -> n #

(k ~ An_Iso, a ~ n, b ~ n) => LabelOptic "unBytes" k (Bytes s n) (Bytes s n) a b Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

labelOptic :: Optic k NoIx (Bytes s n) (Bytes s n) a b Source #

Applicative (Bytes s) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

pure :: a -> Bytes s a #

(<*>) :: Bytes s (a -> b) -> Bytes s a -> Bytes s b #

liftA2 :: (a -> b -> c) -> Bytes s a -> Bytes s b -> Bytes s c #

(*>) :: Bytes s a -> Bytes s b -> Bytes s b #

(<*) :: Bytes s a -> Bytes s b -> Bytes s a #

Functor (Bytes s) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

fmap :: (a -> b) -> Bytes s a -> Bytes s b #

(<$) :: a -> Bytes s b -> Bytes s a #

Monad (Bytes s) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(>>=) :: Bytes s a -> (a -> Bytes s b) -> Bytes s b #

(>>) :: Bytes s a -> Bytes s b -> Bytes s b #

return :: a -> Bytes s a #

Foldable (Bytes s) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

fold :: Monoid m => Bytes s m -> m #

foldMap :: Monoid m => (a -> m) -> Bytes s a -> m #

foldMap' :: Monoid m => (a -> m) -> Bytes s a -> m #

foldr :: (a -> b -> b) -> b -> Bytes s a -> b #

foldr' :: (a -> b -> b) -> b -> Bytes s a -> b #

foldl :: (b -> a -> b) -> b -> Bytes s a -> b #

foldl' :: (b -> a -> b) -> b -> Bytes s a -> b #

foldr1 :: (a -> a -> a) -> Bytes s a -> a #

foldl1 :: (a -> a -> a) -> Bytes s a -> a #

toList :: Bytes s a -> [a] #

null :: Bytes s a -> Bool #

length :: Bytes s a -> Int #

elem :: Eq a => a -> Bytes s a -> Bool #

maximum :: Ord a => Bytes s a -> a #

minimum :: Ord a => Bytes s a -> a #

sum :: Num a => Bytes s a -> a #

product :: Num a => Bytes s a -> a #

Traversable (Bytes s) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

traverse :: Applicative f => (a -> f b) -> Bytes s a -> f (Bytes s b) #

sequenceA :: Applicative f => Bytes s (f a) -> f (Bytes s a) #

mapM :: Monad m => (a -> m b) -> Bytes s a -> m (Bytes s b) #

sequence :: Monad m => Bytes s (m a) -> m (Bytes s a) #

AGroup n => AGroup (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.-.) :: Bytes s n -> Bytes s n -> Bytes s n Source #

AMonoid n => AMonoid (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

zero :: Bytes s n Source #

ASemigroup n => ASemigroup (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.+.) :: Bytes s n -> Bytes s n -> Bytes s n Source #

Normed n => Normed (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

norm :: Bytes s n -> Bytes s n Source #

FromInteger n => FromInteger (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

afromInteger :: Integer -> Bytes s n Source #

FromRational n => FromRational (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

AnyLowerBounded n => AnyLowerBounded (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

AnyUpperBounded n => AnyUpperBounded (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

LowerBounded n => LowerBounded (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

lowerBound :: Bytes s n Source #

LowerBoundless (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

UpperBounded n => UpperBounded (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

upperBound :: Bytes s n Source #

UpperBoundless (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

NFData n => NFData (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

rnf :: Bytes s n -> () #

Bounded n => Bounded (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

minBound :: Bytes s n #

maxBound :: Bytes s n #

Generic (Bytes s n) Source # 
Instance details

Defined in Data.Bytes.Internal

Associated Types

type Rep (Bytes s n)

Since: si-bytes-0.1

Instance details

Defined in Data.Bytes.Internal

type Rep (Bytes s n) = D1 ('MetaData "Bytes" "Data.Bytes.Internal" "si-bytes-0.1-inplace" 'True) (C1 ('MetaCons "MkBytes" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 n)))

Methods

from :: Bytes s n -> Rep (Bytes s n) x #

to :: Rep (Bytes s n) x -> Bytes s n #

Show n => Show (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

showsPrec :: Int -> Bytes s n -> ShowS #

show :: Bytes s n -> String #

showList :: [Bytes s n] -> ShowS #

Eq n => Eq (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(==) :: Bytes s n -> Bytes s n -> Bool #

(/=) :: Bytes s n -> Bytes s n -> Bool #

Ord n => Ord (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

compare :: Bytes s n -> Bytes s n -> Ordering #

(<) :: Bytes s n -> Bytes s n -> Bool #

(<=) :: Bytes s n -> Bytes s n -> Bool #

(>) :: Bytes s n -> Bytes s n -> Bool #

(>=) :: Bytes s n -> Bytes s n -> Bool #

max :: Bytes s n -> Bytes s n -> Bytes s n #

min :: Bytes s n -> Bytes s n -> Bytes s n #

Hashable n => Hashable (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

hashWithSalt :: Int -> Bytes s n -> Int Source #

hash :: Bytes s n -> Int 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, Normed n, Ord n, SingSize s) => Normalize (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type Norm (Bytes s n) 
Instance details

Defined in Data.Bytes.Internal

type Norm (Bytes s n) = SomeSize n

Methods

normalize :: Bytes s n -> Norm (Bytes s n) Source #

Read n => Parser (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

parser :: Parsec Void Text (Bytes s n) Source #

RawNumeric (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type Raw (Bytes s n) 
Instance details

Defined in Data.Bytes.Internal

type Raw (Bytes s n) = n

Methods

toRaw :: Bytes s n -> Raw (Bytes s n) Source #

SingSize s => Sized (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type HideSize (Bytes s n) 
Instance details

Defined in Data.Bytes.Internal

type HideSize (Bytes s n) = SomeSize n

Methods

sizeOf :: Bytes s n -> Size Source #

hideSize :: Bytes s n -> HideSize (Bytes s n) Source #

MSemigroup n => MSemiSpace (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.*) :: Bytes s n -> n -> Bytes s n Source #

MGroup n => MSpace (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.%) :: Bytes s n -> n -> Bytes s n Source #

Ring n => Module (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Semiring n => Semimodule (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Semifield n => SemivectorSpace (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Field n => VectorSpace (Bytes s n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

type Converted t (Bytes s n) Source # 
Instance details

Defined in Data.Bytes.Internal

type Converted t (Bytes s n) = Bytes t n
type Rep (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

type Rep (Bytes s n) = D1 ('MetaData "Bytes" "Data.Bytes.Internal" "si-bytes-0.1-inplace" 'True) (C1 ('MetaCons "MkBytes" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 n)))
type Norm (Bytes s n) Source # 
Instance details

Defined in Data.Bytes.Internal

type Norm (Bytes s n) = SomeSize n
type Raw (Bytes s n) Source # 
Instance details

Defined in Data.Bytes.Internal

type Raw (Bytes s n) = n
type HideSize (Bytes s n) Source # 
Instance details

Defined in Data.Bytes.Internal

type HideSize (Bytes s n) = SomeSize n

_MkBytes :: forall (s :: Size) n. Iso' (Bytes s n) n Source #

Iso' between Bytes and underlying value.

Examples

Expand
>>> import Optics.Core (review, view)
>>> (review _MkBytes 70) :: Bytes K Int
MkBytes 70
>>> view _MkBytes (MkBytes @K @Int 70)
70

Since: 0.1

bytesToSSize :: forall (s :: Size) n. SingSize s => Bytes s n -> SSize s Source #

Retrieves the SSize witness. Can be used to recover the Size.

>>> bytesToSSize (MkBytes @K @Int 7)
SK

Since: 0.1

Unknown Size

data SomeSize n where Source #

Wrapper for Bytes, existentially quantifying the size. This is useful when a function does not know a priori what size it should return e.g.

>>> :{
  getFileSize :: FilePath -> IO (SomeSize Float)
  getFileSize path = do
    -- getRawFileSize :: FilePath -> IO (Float, String)
    (bytes, units) <- getRawFileSize path
    pure $ case units of
      "B" -> hideSize $ MkBytes @B bytes
      "K" -> hideSize $ MkBytes @K bytes
      _ -> error "todo"
:}

We define an equivalence relation on SomeSize that takes units into account. For instance,

>>> hideSize (MkBytes @G 7) == hideSize (MkBytes @M 7_000)
True

Because we expose the underlying Bytes in several ways (e.g. Show, the SSize witness), this is technically unlawful for equality as it breaks the extensionality law:

\[ x = y \implies f(x) = f(y). \]

Since: 0.1

Constructors

MkSomeSize :: forall (s :: Size) n. SSize s -> Bytes s n -> SomeSize n

Since: 0.1

Instances

Instances details
Functor SomeSize Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

fmap :: (a -> b) -> SomeSize a -> SomeSize b #

(<$) :: a -> SomeSize b -> SomeSize a #

Foldable SomeSize Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

fold :: Monoid m => SomeSize m -> m #

foldMap :: Monoid m => (a -> m) -> SomeSize a -> m #

foldMap' :: Monoid m => (a -> m) -> SomeSize a -> m #

foldr :: (a -> b -> b) -> b -> SomeSize a -> b #

foldr' :: (a -> b -> b) -> b -> SomeSize a -> b #

foldl :: (b -> a -> b) -> b -> SomeSize a -> b #

foldl' :: (b -> a -> b) -> b -> SomeSize a -> b #

foldr1 :: (a -> a -> a) -> SomeSize a -> a #

foldl1 :: (a -> a -> a) -> SomeSize a -> a #

toList :: SomeSize a -> [a] #

null :: SomeSize a -> Bool #

length :: SomeSize a -> Int #

elem :: Eq a => a -> SomeSize a -> Bool #

maximum :: Ord a => SomeSize a -> a #

minimum :: Ord a => SomeSize a -> a #

sum :: Num a => SomeSize a -> a #

product :: Num a => SomeSize a -> a #

Traversable SomeSize Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

traverse :: Applicative f => (a -> f b) -> SomeSize a -> f (SomeSize b) #

sequenceA :: Applicative f => SomeSize (f a) -> f (SomeSize a) #

mapM :: Monad m => (a -> m b) -> SomeSize a -> m (SomeSize b) #

sequence :: Monad m => SomeSize (m a) -> m (SomeSize a) #

HasField "unSomeSize" (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

getField :: SomeSize n -> n #

(k ~ A_Getter, a ~ n, b ~ n) => LabelOptic "unSomeSize" k (SomeSize n) (SomeSize n) a b Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

labelOptic :: Optic k NoIx (SomeSize n) (SomeSize n) a b Source #

(Field n, FromInteger n) => AGroup (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.-.) :: SomeSize n -> SomeSize n -> SomeSize n Source #

(FromInteger n, Semifield n) => AMonoid (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

zero :: SomeSize n Source #

(ASemigroup n, FromInteger n, MGroup n) => ASemigroup (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.+.) :: SomeSize n -> SomeSize n -> SomeSize n Source #

Normed n => Normed (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

norm :: SomeSize n -> SomeSize n Source #

FromInteger n => FromInteger (SomeSize n) Source #

Fixed size B.

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

FromRational n => FromRational (SomeSize n) Source #

Fixed size B.

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

NFData n => NFData (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

rnf :: SomeSize n -> () #

Show n => Show (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

showsPrec :: Int -> SomeSize n -> ShowS #

show :: SomeSize n -> String #

showList :: [SomeSize n] -> ShowS #

(Eq n, FromInteger n, MGroup n) => Eq (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(==) :: SomeSize n -> SomeSize n -> Bool #

(/=) :: SomeSize n -> SomeSize n -> Bool #

(FromInteger n, MGroup n, Ord n) => Ord (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

compare :: SomeSize n -> SomeSize n -> Ordering #

(<) :: SomeSize n -> SomeSize n -> Bool #

(<=) :: SomeSize n -> SomeSize n -> Bool #

(>) :: SomeSize n -> SomeSize n -> Bool #

(>=) :: SomeSize n -> SomeSize n -> Bool #

max :: SomeSize n -> SomeSize n -> SomeSize n #

min :: SomeSize n -> SomeSize n -> SomeSize n #

(FromInteger n, Hashable n, MGroup n) => Hashable (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

(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, Normed n, Ord n) => Normalize (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type Norm (SomeSize n) 
Instance details

Defined in Data.Bytes.Internal

type Norm (SomeSize n) = SomeSize n

Methods

normalize :: SomeSize n -> Norm (SomeSize n) Source #

Read n => Parser (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

RawNumeric (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type Raw (SomeSize n) 
Instance details

Defined in Data.Bytes.Internal

type Raw (SomeSize n) = n

Methods

toRaw :: SomeSize n -> Raw (SomeSize n) Source #

Sized (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type HideSize (SomeSize n) 
Instance details

Defined in Data.Bytes.Internal

MGroup n => MSemiSpace (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.*) :: SomeSize n -> n -> SomeSize n Source #

MGroup n => MSpace (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Methods

(.%) :: SomeSize n -> n -> SomeSize n Source #

(Field n, FromInteger n) => Module (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

(FromInteger n, Semifield n) => Semimodule (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

(FromInteger n, Semifield n) => SemivectorSpace (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

(Field n, FromInteger n) => VectorSpace (SomeSize n) n Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

type Converted t (SomeSize n) Source # 
Instance details

Defined in Data.Bytes.Internal

type Converted t (SomeSize n) = Bytes t n
type Norm (SomeSize n) Source # 
Instance details

Defined in Data.Bytes.Internal

type Norm (SomeSize n) = SomeSize n
type Raw (SomeSize n) Source # 
Instance details

Defined in Data.Bytes.Internal

type Raw (SomeSize n) = n
type HideSize (SomeSize n) Source # 
Instance details

Defined in Data.Bytes.Internal

_MkSomeSize :: forall (s :: Size) n. (FromInteger n, MGroup n, SingSize s) => Iso' (SomeSize n) (Bytes s n) Source #

Iso' between SomeSize and underlying Bytes. Performs any necessary conversions when going from SomeSize n -> Bytes s n.

Examples

Expand
>>> import Optics.Core (review, view)
>>> review _MkSomeSize (MkBytes @K @Int 70)
MkSomeSize SK (MkBytes 70)
>>> (view _MkSomeSize (hideSize $ MkBytes @K @Int 70)) :: Bytes B Int
MkBytes 70000

Since: 0.1