-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed benchmark.
- Loading branch information
Showing
2 changed files
with
48 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,55 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE PolyKinds #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
module Data.Vinyl.Derived where | ||
|
||
import Data.Proxy | ||
import Data.Vinyl.Core | ||
import Data.Vinyl.Functor | ||
|
||
import Foreign.Ptr (castPtr) | ||
import Foreign.Storable | ||
import GHC.TypeLits | ||
|
||
data ElField (field :: (Symbol, *)) where | ||
Field :: KnownSymbol s => t -> ElField '(s,t) | ||
Field :: KnownSymbol s => !t -> ElField '(s,t) | ||
|
||
type FieldRec = Rec ElField | ||
type HList = Rec Identity | ||
type LazyHList = Rec Thunk | ||
|
||
deriving instance Eq t => Eq (ElField '(s,t)) | ||
deriving instance Ord t => Ord (ElField '(s,t)) | ||
|
||
instance Show t => Show (ElField '(s,t)) where | ||
show (Field x) = (symbolVal (Proxy::Proxy s))++" :-> "++show x | ||
|
||
-- | Get the data payload of an 'ElField'. | ||
getField :: ElField '(s,t) -> t | ||
getField (Field x) = x | ||
|
||
-- | 'ElField' is isomorphic to a functor something like @Compose | ||
-- ElField ('(,) s)@. | ||
fieldMap :: (a -> b) -> ElField '(s,a) -> ElField '(s,b) | ||
fieldMap f (Field x) = Field (f x) | ||
{-# INLINE fieldMap #-} | ||
|
||
-- | Lens for an 'ElField''s data payload. | ||
rfield :: Functor f => (a -> f b) -> ElField '(s,a) -> f (ElField '(s,b)) | ||
rfield f (Field x) = fmap Field (f x) | ||
{-# INLINE rfield #-} | ||
|
||
-- | Shorthand for a 'FieldRec' with a single field. | ||
(=::) :: KnownSymbol s => proxy '(s,a) -> a -> FieldRec '[ '(s,a) ] | ||
(=::) _ x = Field x :& RNil | ||
|
||
instance forall s t. (KnownSymbol s, Storable t) | ||
=> Storable (ElField '(s,t)) where | ||
sizeOf _ = sizeOf (undefined::t) | ||
alignment _ = alignment (undefined::t) | ||
peek ptr = Field `fmap` peek (castPtr ptr) | ||
poke ptr (Field x) = poke (castPtr ptr) x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2f23ead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jonsterling, can you look this over? I need a patch release pushed to hackage with at least the intent of these changes so I can push a new
vinyl-gl
.A couple notes:
'[ '("Foo", Foo) ]
. This isn't a deal breaker, but just to note a drawback to using the tuple kind for these purposes.Rec
creation operator(=:)
is used at a custom type in the tutorial, so I took(=::)
forElField
. This seems a bit of tail wagging the dog to me, but I know you're not really a fan ofElField
, so perhaps it is consistent with your aims. I don't feel too strongly, so it's your call.Field
constructor strict in its argument.2f23ead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2f23ead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, just realized this is not a PR. That's fine too :)
Let me know when you are ready for me to cut a new version.
2f23ead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it should have been a PR. It started out as just a couple instances so I did it on master and forgot I wasn't on a branch.
2f23ead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem at all!