Hey Everyone ! Beginner here! May be something super obvious that I am missing .
Disclaimer : I have already looked at How to implement the trait std::convert::AsRef<[u8]> for generic structure but this situation is different as I have implemented (hopefully correctly) the trait but it still won't compile.
I'm trying to make a merkle tree of hashes. I am using Seahorse and trying to import the merkle tree in the Anchor code :
use std::{cell::RefCell, rc::Rc};
use anchor_spl::{
associated_token::{self, AssociatedToken},
token::{self, Mint, Token, TokenAccount},
};
use crate::{id, seahorse_util::*};
use anchor_lang::{prelude::*, solana_program};
use solana_merkle_tree::merkle_tree;
pub trait AsRef {
fn as_ref(&self) -> &[u8];
}
impl AsRef for [u8]
{
fn as_ref(&self) -> &[u8] {
return std::convert::AsRef::as_ref(&self);
}
}
#[account]
#[derive(Debug, PartialEq, Copy)]
pub struct Calculator {
pub owner: Pubkey,
pub display: u8,
pub hash: [u8; 32]
}
This is the part where I am trying to create a merkle tree
merkle_tree::MerkleTree::new(&mut *calculator.borrow_mut().hash.borrow_mut());
This is the compiler message:
stack@DESKTOP-MLB9OKV:~/desktop/seahorse_anchor_solana/calculator$ anchor build
Warning: cargo-build-bpf is deprecated. Please, use cargo-build-sbf
cargo-build-bpf child: /home/stack/.local/share/solana/install/active_release/bin/cargo-build-sbf --arch bpf
Compiling calculator v0.1.0 (/mnt/c/Users/Stack/Desktop/seahorse_anchor_solana/calculator/programs/calculator)
error[E0277]: the trait bound `u8: std::convert::AsRef<[u8]>` is not satisfied
--> programs/calculator/src/dot/program.rs:164:50
|
164 | merkle_tree::MerkleTree::new(&mut *calculator.borrow_mut().hash.borrow_mut());
| ---------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::AsRef<[u8]>` is not implemented for `u8`
| |
| required by a bound introduced by this call
|
note: required by a bound in `MerkleTree::new`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `calculator` due to previous error
I have also tried to specifically use
merkle_tree::MerkleTree::new(std::convert::AsRef::as_ref(&calculator.borrow_mut().hash.borrow_mut()));
But with no success.
This error arises even at a string which definately supports AsRef:
merkle_tree::MerkleTree::new(&b"this_string_is_32_chars_long_now");