Skip to content

get_mark could be an associated constant #6

@tob-joe

Description

@tob-joe

Here's a diff:

diff --git a/inscribe-derive/src/lib.rs b/inscribe-derive/src/lib.rs
index 5c4a976..ef5dc93 100644
--- a/inscribe-derive/src/lib.rs
+++ b/inscribe-derive/src/lib.rs
@@ -122,7 +122,7 @@ fn implement_get_inscription(dstruct: &DataStruct) -> TokenStream {
                 use decree::decree::FSInput;

                 let mut serial_out: Vec<u8> = Vec::new();
-                let mut hasher = TupleHash::v256(self.get_mark().as_bytes());
+                let mut hasher = TupleHash::v256(Self::MARK.as_bytes());

                 // Add the struct members into the TupleHash
                 #center
@@ -139,22 +139,20 @@ fn implement_get_inscription(dstruct: &DataStruct) -> TokenStream {
 }


-fn implement_get_mark(ast: DeriveInput) -> TokenStream {
+fn implement_mark(ast: DeriveInput) -> TokenStream {
     // By default, the mark/identifier for a struct will be its name
     let ident = ast.ident;
     let ident_str = ident.to_string();

-    let get_mark = quote!{
-            fn get_mark(&self) -> &'static str {
-                return #ident_str;
-            }
+    let mark = quote!{
+            const MARK: &'static str = #ident_str;
         };
-    get_mark
+    mark
 }


 fn implement_inscribe_trait(ast: DeriveInput, dstruct: &DataStruct) -> TokenStream {
-    let get_mark: TokenStream = implement_get_mark(ast.clone());
+    let mark: TokenStream = implement_mark(ast.clone());
     let get_inscr: TokenStream = implement_get_inscription(dstruct);
     let get_addl: TokenStream = implement_get_addl(ast.clone());

@@ -164,7 +162,7 @@ fn implement_inscribe_trait(ast: DeriveInput, dstruct: &DataStruct) -> TokenStre
     quote! {
         impl #generics Inscribe for #ident #generics {

-            #get_mark
+            #mark

             #get_inscr

diff --git a/src/inscribe.rs b/src/inscribe.rs
index c330cec..01d94da 100644
--- a/src/inscribe.rs
+++ b/src/inscribe.rs
@@ -8,14 +8,14 @@ pub type InscribeBuffer = [u8; INSCRIBE_LENGTH];
 /// contextual data into Fiat-Shamir transcripts. There are two main methods that the trait
 /// requires:
 ///
-/// `fn get_mark(&self) -> &'static str`
+/// `const MARK: &'static str`
 ///
 /// and
 ///
 /// `fn get_inscription(&self) -> FSInput`
 ///
 /// For derived structs, the `get_inscription` method will do the following:
-///     - Initialize a TupleHash with the results of `get_mark`
+///     - Initialize a TupleHash with the contents of `MARK``
 ///     - For each member of the struct, do one of three things:
 ///         + For `Inscribe` implementers, call `get_inscription` and add the results to the
 ///             TupleHash
@@ -110,7 +110,7 @@ pub type InscribeBuffer = [u8; INSCRIBE_LENGTH];
 /// ```
 ///
 pub trait Inscribe {
-    fn get_mark(&self) -> &'static str;
+    const MARK: &'static str;
     fn get_inscription(&self) -> DecreeResult<FSInput>;
     fn get_additional(&self) -> DecreeResult<FSInput> {
         let x: Vec<u8> = Vec::new();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions