From 98841c5fe3d814222029ac32080ddf4342fdf9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20van=20Br=C3=BCgge?= Date: Sun, 8 Jun 2025 14:53:26 +0200 Subject: [PATCH] Add Binary instance for Generically newtype --- src/Data/Binary/Class.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Data/Binary/Class.hs b/src/Data/Binary/Class.hs index 0b79743..fde3e8f 100644 --- a/src/Data/Binary/Class.hs +++ b/src/Data/Binary/Class.hs @@ -9,6 +9,11 @@ {-# LANGUAGE PolyKinds #-} #endif +#if MIN_VERSION_base(4,17,0) +{-# LANGUAGE UndecidableInstances #-} +#define HAS_GENERICALLY_NEWTYPE +#endif + #if MIN_VERSION_base(4,16,0) #define HAS_TYPELITS_CHAR #endif @@ -181,6 +186,12 @@ defaultPutList xs = put (length xs) <> mapM_ put xs ------------------------------------------------------------------------ -- Simple instances +#ifdef HAS_GENERICALLY_NEWTYPE +instance (Generic t, GBinaryPut (Rep t), GBinaryGet (Rep t)) => Binary (Generically t) where + put (Generically x) = gput $ from x + get = (Generically . to) `fmap` gget +#endif + #ifdef HAS_VOID -- Void never gets written nor reconstructed since it's impossible to have a -- value of that type