|
| 1 | +-- | `DataView` represents unaligned memory of unknown endianness. |
| 2 | +-- | |
| 3 | +-- | `ArrayView` represents arrays of aligned elements of |
| 4 | +-- | local-machine endianness. |
| 5 | +-- | For the cases of `Int8Array`, `Uint8Array`, `Uint8ClampedArray`, |
| 6 | +-- | the elements |
| 7 | +-- | are single bytes, so they are always aligned and they have no |
| 8 | +-- | endianness. Therefore in those cases we can freely cast back and forth |
| 9 | +-- | to `DataView`. |
| 10 | +module Data.ArrayBuffer.Cast |
| 11 | + ( fromInt8Array |
| 12 | + , fromUint8Array |
| 13 | + , fromUint8ClampedArray |
| 14 | + , toInt8Array |
| 15 | + , toUint8Array |
| 16 | + , toUint8ClampedArray |
| 17 | + ) where |
| 18 | + |
| 19 | +import Data.ArrayBuffer.DataView as DV |
| 20 | +import Data.ArrayBuffer.Typed as AT |
| 21 | +import Data.ArrayBuffer.Types (DataView, Uint8Array, Uint8ClampedArray, Int8Array) |
| 22 | +import Effect (Effect) |
| 23 | + |
| 24 | +-- | Cast an `Int8Array` to a `DataView`. |
| 25 | +fromInt8Array :: Int8Array -> Effect DataView |
| 26 | +fromInt8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) |
| 27 | + |
| 28 | +-- | Cast a `DataView` to an `Int8Array`. |
| 29 | +toInt8Array :: DataView -> Effect Int8Array |
| 30 | +toInt8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) |
| 31 | + |
| 32 | +-- | Cast a `UInt8Array` to a `DataView`. |
| 33 | +fromUint8Array :: Uint8Array -> Effect DataView |
| 34 | +fromUint8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) |
| 35 | + |
| 36 | +-- | Cast a `DataView` to a `Uint8Array`. |
| 37 | +toUint8Array :: DataView -> Effect Uint8Array |
| 38 | +toUint8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) |
| 39 | + |
| 40 | +-- | Cast a `UInt8ClampedArray` to a `DataView`. |
| 41 | +fromUint8ClampedArray :: Uint8ClampedArray -> Effect DataView |
| 42 | +fromUint8ClampedArray x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) |
| 43 | + |
| 44 | +-- | Cast a `DataView` to a `Uint8ClampedArray`. |
| 45 | +toUint8ClampedArray :: DataView -> Effect Uint8ClampedArray |
| 46 | +toUint8ClampedArray x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) |
0 commit comments