From dd5f7396306612665babf751eda5d472470f5496 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Thu, 13 Feb 2025 16:35:56 +0000 Subject: [PATCH] radicle: generic Transaction::initial The use of `Transaction` outside of the `radicle` crate requires that a newtype is used, if the upstream code wants to write methods, such as: ``` pub struct Transaction(store::Transaction); impl Transaction { pub fn add_foo(&mut self, foo: Foo) -> Result<(), store::Error> { /* code */ } pub fn add_bar(&mut self, bar: Bar) -> Result<(), store::Error> { /* code */ }} ``` This works up until the point that it tries to re-use the `Transaction::initial` method. To make this possible, the method needs to know that it can be converted to the upstream `Transaction` and back to `radicle` `Transaction`. --- radicle/src/cob/store.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/radicle/src/cob/store.rs b/radicle/src/cob/store.rs index 058f92b8..791e8bae 100644 --- a/radicle/src/cob/store.rs +++ b/radicle/src/cob/store.rs @@ -314,20 +314,23 @@ where T: Cob + cob::Evaluate, { /// Create a new transaction to be used as the initial set of operations for a COB. - pub fn initial( + pub fn initial( message: &str, store: &mut Store, signer: &G, operations: F, ) -> Result<(ObjectId, T), Error> where + Tx: From, + Self: From, G: Signer, - F: FnOnce(&mut Self, &R) -> Result<(), Error>, + F: FnOnce(&mut Tx, &R) -> Result<(), Error>, R: ReadRepository + SignRepository + cob::Store, T::Action: Serialize + Clone, { - let mut tx = Transaction::default(); + let mut tx = Tx::from(Transaction::default()); operations(&mut tx, store.as_ref())?; + let tx = Self::from(tx); let actions = NonEmpty::from_vec(tx.actions) .expect("Transaction::initial: transaction must contain at least one action");