Safe Haskell | None |
---|---|
Language | Haskell2010 |
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
- newtype Bytes (s :: Size) n = MkBytes n
- _MkBytes :: forall (s :: Size) n. Iso' (Bytes s n) n
- bytesToSSize :: forall (s :: Size) n. SingSize s => Bytes s n -> SSize s
- data SomeSize n where
- MkSomeSize :: forall (s :: Size) n. SSize s -> Bytes s n -> SomeSize n
- _MkSomeSize :: forall (s :: Size) n. (FromInteger n, MGroup n, SingSize s) => Iso' (SomeSize n) (Bytes s n)
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
>>>
MkBytes @M 1000
MkBytes 1000
Since: 0.1
MkBytes n |
Instances
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
MkSomeSize :: forall (s :: Size) n. SSize s -> Bytes s n -> SomeSize n | Since: 0.1 |
Instances
Functor SomeSize Source # | Since: 0.1 |
Foldable SomeSize Source # | Since: 0.1 |
Defined in Data.Bytes.Internal 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 # elem :: Eq a => a -> SomeSize a -> Bool # maximum :: Ord a => SomeSize a -> a # minimum :: Ord a => SomeSize a -> a # | |
Traversable SomeSize Source # | Since: 0.1 |
HasField "unSomeSize" (SomeSize n) n Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
(k ~ A_Getter, a ~ n, b ~ n) => LabelOptic "unSomeSize" k (SomeSize n) (SomeSize n) a b Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
(Field n, FromInteger n) => AGroup (SomeSize n) Source # | Since: 0.1 |
(FromInteger n, Semifield n) => AMonoid (SomeSize n) Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
(ASemigroup n, FromInteger n, MGroup n) => ASemigroup (SomeSize n) Source # | Since: 0.1 |
Normed n => Normed (SomeSize n) Source # | Since: 0.1 |
FromInteger n => FromInteger (SomeSize n) Source # | Fixed size Since: 0.1 |
Defined in Data.Bytes.Internal afromInteger :: Integer -> SomeSize n Source # | |
FromRational n => FromRational (SomeSize n) Source # | Fixed size Since: 0.1 |
Defined in Data.Bytes.Internal afromRational :: Rational -> SomeSize n Source # | |
NFData n => NFData (SomeSize n) Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
Show n => Show (SomeSize n) Source # | Since: 0.1 |
(Eq n, FromInteger n, MGroup n) => Eq (SomeSize n) Source # | Since: 0.1 |
(FromInteger n, MGroup n, Ord n) => Ord (SomeSize n) Source # | Since: 0.1 |
(FromInteger n, Hashable n, MGroup n) => Hashable (SomeSize n) Source # | Since: 0.1 |
(FromInteger n, MGroup n) => Conversion (SomeSize n) Source # | Since: 0.1 |
(FromInteger n, MGroup n, Normed n, Ord n) => Normalize (SomeSize n) Source # | Since: 0.1 |
Read n => Parser (SomeSize n) Source # | Since: 0.1 |
RawNumeric (SomeSize n) Source # | Since: 0.1 |
Sized (SomeSize n) Source # | Since: 0.1 |
MGroup n => MSemiSpace (SomeSize n) n Source # | Since: 0.1 |
MGroup n => MSpace (SomeSize n) n Source # | Since: 0.1 |
(Field n, FromInteger n) => Module (SomeSize n) n Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
(FromInteger n, Semifield n) => Semimodule (SomeSize n) n Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
(FromInteger n, Semifield n) => SemivectorSpace (SomeSize n) n Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
(Field n, FromInteger n) => VectorSpace (SomeSize n) n Source # | Since: 0.1 |
Defined in Data.Bytes.Internal | |
type Converted t (SomeSize n) Source # | |
Defined in Data.Bytes.Internal | |
type Norm (SomeSize n) Source # | |
Defined in Data.Bytes.Internal | |
type Raw (SomeSize n) Source # | |
Defined in Data.Bytes.Internal | |
type HideSize (SomeSize n) Source # | |
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
>>>
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