Initial
This commit is contained in:
commit
62ff812bdb
5 changed files with 175 additions and 0 deletions
45
.circleci/config.yml
Normal file
45
.circleci/config.yml
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
version: 2.1
|
||||
|
||||
jobs:
|
||||
build-linux:
|
||||
parameters:
|
||||
version:
|
||||
type: string
|
||||
docker:
|
||||
- image: haskell:<< parameters.version >>
|
||||
steps:
|
||||
- checkout
|
||||
- restore_cache:
|
||||
name: Restore
|
||||
key: cache-<< parameters.version >>-{{ checksum "storable-offset.cabal" }}
|
||||
- run:
|
||||
name: Update Dependencies
|
||||
command: |
|
||||
cabal new-update && if [[ $(cabal --numeric-version) == '2.2.0.0' ]];
|
||||
then cabal new-install
|
||||
else cabal new-install --lib
|
||||
fi
|
||||
- run:
|
||||
name: Build
|
||||
command: cabal new-build
|
||||
- save_cache:
|
||||
name: Cache
|
||||
key: cache-<< parameters.version >>-{{ checksum "storable-offset.cabal" }}
|
||||
paths:
|
||||
- "/root/.cabal"
|
||||
- "dist-newstyle"
|
||||
|
||||
|
||||
|
||||
workflows:
|
||||
workflow:
|
||||
jobs:
|
||||
- build-linux:
|
||||
name: linux-8.2.2
|
||||
version: 8.2.2
|
||||
- build-linux:
|
||||
name: linux-8.8.4
|
||||
version: 8.8.4
|
||||
- build-linux:
|
||||
name: linux-9.2.2
|
||||
version: 9.2.2-slim
|
||||
30
LICENSE
Normal file
30
LICENSE
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
Copyright Oleksii Divak (c) 2022
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the name of Author name here nor the names of other
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
22
README.md
Normal file
22
README.md
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# storable-offset
|
||||
|
||||
Tiny helper library that provides a way to access fields of `Storable` data structures.
|
||||
|
||||
Given an instance
|
||||
|
||||
```haskell
|
||||
{-# LANGUAGE DataKinds
|
||||
, MultiParamTypeClasses #-}
|
||||
|
||||
instance Offset "fieldName" DataType where rawOffset = #{offset data_type, field_name}
|
||||
```
|
||||
|
||||
, you can now `peek`/`poke` `fieldName` directly using
|
||||
|
||||
```haskell
|
||||
{-# LANGUAGE TypeApplications #-}
|
||||
|
||||
peek $ offset @"fieldName" (ptr @DataType)
|
||||
|
||||
poke (offset @"fieldName" ptr) (val @DataType)
|
||||
```
|
||||
45
src/Foreign/Storable/Offset.hs
Normal file
45
src/Foreign/Storable/Offset.hs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{-# LANGUAGE AllowAmbiguousTypes
|
||||
, DataKinds
|
||||
, KindSignatures
|
||||
, MultiParamTypeClasses
|
||||
, Rank2Types
|
||||
, ScopedTypeVariables
|
||||
, TypeApplications #-}
|
||||
|
||||
{- | Utility functions for using 'Storable' to access record fields of data structures.
|
||||
|
||||
Functions in this module rely on the [TypeApplications](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/type_applications.html) pragma (the 'Offset' class itself doesn't).
|
||||
-}
|
||||
|
||||
module Foreign.Storable.Offset
|
||||
( -- * Offset
|
||||
Offset (..)
|
||||
, offset
|
||||
-- * Helper functions
|
||||
, pokeField
|
||||
) where
|
||||
|
||||
import Foreign.Ptr
|
||||
import Foreign.Storable
|
||||
import GHC.Records
|
||||
import GHC.TypeLits
|
||||
|
||||
|
||||
|
||||
class Offset (x :: Symbol) r where
|
||||
-- | Byte distance between the start of datatype @r@ within memory and its field @x@.
|
||||
rawOffset :: Int
|
||||
|
||||
-- | Advances a pointer based on field's 'Offset', inheriting field's type.
|
||||
--
|
||||
-- Resulting pointer can be used to 'peek' or 'poke' the field directly.
|
||||
offset :: forall x r a. (HasField x r a, Offset x r) => Ptr r -> Ptr a
|
||||
offset = (`plusPtr` rawOffset @x @r)
|
||||
|
||||
|
||||
|
||||
-- | Retrieves the field and 'poke's it at proper 'offset'.
|
||||
--
|
||||
-- Useful for declaring 'poke' for the entire datatype.
|
||||
pokeField :: forall x r a. (HasField x r a, Offset x r, Storable a) => Ptr r -> r -> IO ()
|
||||
pokeField ptr = poke (offset @x @r ptr) . getField @x @r
|
||||
33
storable-offset.cabal
Normal file
33
storable-offset.cabal
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
cabal-version: 1.12
|
||||
|
||||
name: storable-offset
|
||||
version: 0.1.0.0
|
||||
description: Please see the README on GitHub at <https://github.com/burningwitness/storable-offset#readme>
|
||||
homepage: https://github.com/burningwitness/storable-offset#readme
|
||||
bug-reports: https://github.com/burningwitness/storable-offset/issues
|
||||
author: Oleksii Divak
|
||||
maintainer: Oleksii Divak <frozenwitness@gmail.com>
|
||||
copyright: 2022 Oleksii Divak
|
||||
license: BSD3
|
||||
license-file: LICENSE
|
||||
category: FFI, Utility
|
||||
synopsis: Storable offsets for record fields
|
||||
build-type: Simple
|
||||
extra-source-files:
|
||||
README.md
|
||||
|
||||
source-repository head
|
||||
type: git
|
||||
location: https://github.com/burningwitness/storable-offset
|
||||
|
||||
library
|
||||
exposed-modules:
|
||||
Foreign.Storable.Offset
|
||||
other-modules:
|
||||
Paths_storable_offset
|
||||
hs-source-dirs:
|
||||
src
|
||||
build-depends:
|
||||
base >=4.10 && <4.17
|
||||
default-language: Haskell2010
|
||||
ghc-options: -Wall
|
||||
Loading…
Add table
Add a link
Reference in a new issue