2

I'm having a hard time pretty-printing the NEAR protocol collections. I believe the best approach is to implement Debug for Map, Set, and Vector. Here's what I think I should be doing:

 use std::fmt;    
 impl fmt::Debug for Map {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 
      // How do I fill this out?
    } 
 }

https://docs.rs/near-sdk/0.10.0/near_sdk/collections/index.html

If this is the wrong approach, how do I use println! to print out the contents of these collections?

mikeDOTexe
  • 487
  • 5
  • 14
Amiya Behera
  • 2,210
  • 19
  • 32
  • Did you file an issue at the github? I'm pretty sure it would be useful to be the library itself – Kitsu Jun 08 '20 at 11:48
  • 1
    The library has to define those for you. See also [How do I implement a trait I don't own for a type I don't own?](https://stackoverflow.com/questions/25413201/how-do-i-implement-a-trait-i-dont-own-for-a-type-i-dont-own). – mcarton Jun 08 '20 at 11:48
  • 1
    Or is you issue "How to implement `Debug`"? It's hard to tell what your problem is. – mcarton Jun 08 '20 at 11:49
  • I looked Debug implementation examples, but could not found how to do it in this case. – Amiya Behera Jun 08 '20 at 12:01
  • 2
    You can't implement a foreign trait for a foregin type – see the question mcarton linked. And while the newtype workaround mentioned there works, you will run into other problems with that workaround. It's best to ask the library author to include `Debug` impls in the lib. – Sven Marnach Jun 08 '20 at 12:39

2 Answers2

3

I believe you're taking a different approach than what you're aiming to do. As I understand it, you want to pretty-print this as you learn how to use these collections. Here are examples of the three collections you mentioned. Using each collections' .to_vec() you can see the results nicely when you run tests.

use near_sdk::{collections::Map, collections::Vector, collections::Set};

…

// you can place this inside a test

let mut my_near_vector: Vector<String> = Vector::new(b"something".to_vec());
my_near_vector.push(&"aloha".to_string());
my_near_vector.push(&"honua".to_string());
println!("Vector {:?}", my_near_vector.to_vec());

let mut my_near_map: Map<String, String> = Map::new(b"it's a dictionary".to_vec());
my_near_map.insert(&"aardvark".to_string(), &"a nocturnal burrowing mammal with long ears".to_string());
my_near_map.insert(&"beelzebub".to_string(), &"a fallen angel in Milton's Paradise Lost".to_string());
println!("Map {:?}", my_near_map.to_vec());

let mut my_near_set: Set<String> = Set::new(b"phonetic alphabet".to_vec());
my_near_set.insert(&"alpha".to_string());
my_near_set.insert(&"bravo".to_string());
println!("Set {:?}", my_near_set.to_vec());

If you then run cargo test -- --nocapture in your project you'll see output like this:

running 1 test
Vector ["aloha", "honua"]
Map [("aardvark", "a nocturnal burrowing mammal with long ears"), ("beelzebub", "a fallen angel in Milton\'s Paradise Lost")]
Set ["alpha", "bravo"]
test tests::demo ... ok
mikeDOTexe
  • 487
  • 5
  • 14
2

Here is the PR adding Debug implementation on the Vector collection. Feel free to add and send PR for adding Debug implementations for other collections.

As pointed out, you cannot implement a foreign trait for a foreign type, so you have 3 options:

  1. Contribute Debug impls to near-sdk
  2. Write a wrapper-types around the collections and impl Debug traits for them
  3. Use .to_vec() / .iter().collect::<HashMap>() methods explicitly and use them to pretty-print
Vlad Frolov
  • 7,445
  • 5
  • 33
  • 52