From 656f43559f488fbee4a4ebd13421136bfe32bcfc Mon Sep 17 00:00:00 2001 From: Maxime Devos Date: Sun, 29 Jan 2023 23:36:29 +0100 Subject: records: New API for record types, specialised to bytevector slices. This simplifies many record definitions and is less prone to errors -- if there is an error in the generation code, it will likely impact multiple record types, so tests for one record type also partially test other record types. It also reduces the amount of code to be written -- reducing boilerplate, in other words. * gnu/gnunet/utils/records.scm: New module. * Makefile.am (modules): Add it. * gnu/gnunet/cadet/client.scm: Use it. * gnu/gnunet/hashcode.scm: Likewise. * gnu/gnunet/fs/uri.scm: Likewise. --- gnu/gnunet/hashcode.scm | 67 ++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 40 deletions(-) (limited to 'gnu/gnunet/hashcode.scm') diff --git a/gnu/gnunet/hashcode.scm b/gnu/gnunet/hashcode.scm index 2137755..d1b9c6b 100644 --- a/gnu/gnunet/hashcode.scm +++ b/gnu/gnunet/hashcode.scm @@ -1,6 +1,6 @@ ;#!r6rs ;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet. -;; Copyright (C) 2006--2020, 2022 GNUnet e.V. +;; Copyright (C) 2006--2020, 2022--2023 GNUnet e.V. ;; ;; GNUnet is free software: you can redistribute it and/or modify it ;; under the terms of the GNU Affero General Public License as published @@ -29,7 +29,10 @@ copy-hashcode:512 copy-hashcode:256) (import (rnrs base) (gnu gnunet utils bv-slice) - (rnrs records syntactic)) + (only (gnu gnunet hashcode struct) + /hashcode:512 /hashcode:256) + (only (gnu gnunet utils records) + define-record-type*)) (define hashcode:512-bit-length 512) (define hashcode:256-bit-length 256) @@ -38,51 +41,35 @@ ;; A 512-bit hashcode. These are the default length for GNUnet, ;; using SHA-512. - (define-record-type ( make-hashcode:512/share hashcode:512?) - (fields (immutable slice hashcode:512->slice)) - (opaque #t) - (sealed #t) - (protocol - (lambda (%make) - (lambda (slice) - "Make a hashcode, containing @var{slice} (a readable + (define-record-type* ( hashcode:512?) + #:network-structure /hashcode:512 + #:read-only-slice-wrapper #true + #:unwrap hashcode:512->slice + #:constructor (make-hashcode:512/share + "Make a hashcode, containing @var{slice} (a readable @code{/hashcode:512} bytevector slice). @var{slice} may not be mutated -while the constructed hashcode is in use." - (assert (= (slice-length slice) hashcode:512-u8-length)) - (%make (slice/read-only slice)))))) - - (define (make-hashcode:512 slice) - "Make a hashcode, containing @var{slice} (a readable @code{/hashcode:512} -bytevector slice). @var{slice} may not be mutated while the constructed -hashcode is in use." - (make-hashcode:512/share (slice-copy/read-only slice))) - - (define (copy-hashcode:512 hashcode:512) - "Make a copy of the hashcode:512 @var{hashcode:512}. This can be useful if +while the constructed hashcode is in use.") + #:constructor/copy make-hashcode:512 + #:copy (copy-hashcode:512 + "Make a copy of the hashcode:512 @var{hashcode:512}. This can be useful if the slice used during the construction of @var{hashcode:512} is potentially -going to be mutated while a hashcode will still be in use." - (make-hashcode:512 (hashcode:512->slice hashcode:512))) +going to be mutated while a hashcode will still be in use.")) ;; A 256-bit hashcode. Used under special conditions, like when space ;; is critical and security is not impacted by it. - (define-record-type ( make-hashcode:256/share hashcode:256?) - (fields (immutable slice hashcode:256->slice)) - (opaque #t) - (sealed #t) - (protocol - (lambda (%make) - (lambda (slice) - "Make a short hashcode, containing @var{slice} (a readable + (define-record-type* ( hashcode:256?) + #:network-structure /hashcode:256 + #:read-only-slice-wrapper #true + #:unwrap hashcode:256->slice + #:constructor (make-hashcode:256/share + "Make a short hashcode, containing @var{slice} (a readable @code{/hashcode:256} bytevector slice). @var{slice} may not be mutated -while the constructed short hashcode is in use." - (assert (= (slice-length slice) hashcode:256-u8-length)) - (%make (slice/read-only slice)))))) - - (define (copy-hashcode:256 hashcode:256) - "Make a copy of the hashcode:256 @var{hashcode:256}. This can be useful if +while the constructed short hashcode is in use.") + #:constructor/copy make-hashcode:256 + #:copy (copy-hashcode:256 + "Make a copy of the hashcode:256 @var{hashcode:256}. This can be useful if the slice used during the construction of @var{hashcode:256} is potentially -going to be mutated while a hashcode will still be in use." - (make-hashcode:256 (hashcode:256->slice hashcode:256))) +going to be mutated while a hashcode will still be in use.")) (define (bv->hashcode:512 bv) "Read a hashcode from a bytevector (deprecated)." -- cgit v1.2.3