You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FYI here's an example from storage-plus... not sure if all of this is necessary, copy/pasted from somewhere
use cosmwasm_schema::cw_serde;use cosmwasm_std::{StdResult,Uint64};use cw_storage_plus::{IntKey,Key,KeyDeserialize,Prefixer,PrimaryKey};use std::{hash::Hash, num::ParseIntError, str::FromStr};/// An id for a task. This is a simple wrapper around a `Uint64` internally./// Serialized on the wire as a string#[cw_serde]#[derive(Copy,PartialOrd,Ord,Eq)]pubstructTaskId(Uint64);implHashforTaskId{fnhash<H: std::hash::Hasher>(&self,state:&mutH){self.u64().hash(state);}}implTaskId{/// Construct a new value from a [u64].pubfnnew(x:u64) -> Self{Self(x.into())}/// The underlying `u64` representation.pubfnu64(self) -> u64{self.0.u64()}}impl<'a>PrimaryKey<'a>forTaskId{typePrefix = ();typeSubPrefix = ();typeSuffix = Self;typeSuperSuffix = Self;fnkey(&self) -> Vec<Key>{vec![Key::Val64(self.0.u64().to_cw_bytes())]}}impl<'a>Prefixer<'a>forTaskId{fnprefix(&self) -> Vec<Key>{vec![Key::Val64(self.0.u64().to_cw_bytes())]}}implKeyDeserializeforTaskId{typeOutput = TaskId;#[inline(always)]fnfrom_vec(value:Vec<u8>) -> StdResult<Self::Output>{
u64::from_vec(value).map(|x| TaskId(Uint64::new(x)))}}impl std::fmt::DisplayforTaskId{fnfmt(&self,f:&mut std::fmt::Formatter) -> std::fmt::Result{write!(f,"{}",self.0)}}implFromStrforTaskId{typeErr = ParseIntError;fnfrom_str(src:&str) -> Result<Self,ParseIntError>{
src.parse().map(|x| TaskId(Uint64::new(x)))}}
@dakom raised this. Sometimes you want to use something with more meaning than
String
(or w/e), and newtype is the right option.Let's make sure it's not overly complicated to implement all the right traits (
OwnedKey
,Key
,std::borrow::Borrow
). Maybe a derive macro is in order.The text was updated successfully, but these errors were encountered: