From d50df3671968e5a489f8245081ef65375f819eec Mon Sep 17 00:00:00 2001 From: Joe Howarth Date: Thu, 3 Feb 2022 14:22:33 -0500 Subject: [PATCH 1/2] Add attribute iterator --- examples/get_accounts.rs | 19 +------------------ src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/examples/get_accounts.rs b/examples/get_accounts.rs index b0cc4cb..f8bfda0 100644 --- a/examples/get_accounts.rs +++ b/examples/get_accounts.rs @@ -20,18 +20,6 @@ use std::{ str::FromStr }; -fn get_attr_str<'a,T>( ite: & mut T ) -> String -where T : Iterator -{ - let mut len = *ite.next().unwrap() as usize; - let mut val = String::with_capacity( len ); - while len > 0 { - val.push( *ite.next().unwrap() as char ); - len -= 1; - } - return val -} - fn get_price_type( ptype: &PriceType ) -> &'static str { match ptype { @@ -78,13 +66,8 @@ fn main() { // print key and reference data for this Product println!( "product_account .. {:?}", prod_pkey ); - let mut psz = prod_acct.size as usize - PROD_HDR_SIZE; - let mut pit = (&prod_acct.attr[..]).iter(); - while psz > 0 { - let key = get_attr_str( &mut pit ); - let val = get_attr_str( &mut pit ); + for (key, val) in prod_acct.iter() { println!( " {:.<16} {}", key, val ); - psz -= 2 + key.len() + val.len(); } // print all Prices that correspond to this Product diff --git a/src/lib.rs b/src/lib.rs index 627e148..e747120 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,6 +128,12 @@ pub struct Product pub attr : [u8;PROD_ATTR_SIZE] } +impl Product { + pub fn iter(&self) -> AttributeIter { + AttributeIter { attrs: &self.attr } + } +} + #[cfg(target_endian = "little")] unsafe impl Zeroable for Product {} @@ -387,3 +393,33 @@ pub fn load_price(data: &[u8]) -> Result<&Price, PythError> { return Ok(pyth_price); } + +struct AttributeIter<'a> { + attrs: &'a [u8], +} + +impl<'a> Iterator for AttributeIter<'a> { + type Item = (&'a str, &'a str); + + fn next(&mut self) -> Option { + if self.attrs.is_empty() { + return None; + } + let (key, data) = get_attr_str(self.attrs); + let (val, data) = get_attr_str(data); + self.attrs = data; + return Some((key, val)); + } +} +// example usage of pyth-client account structure +// bootstrap all product and pricing accounts from root mapping account + +fn get_attr_str(buf: &[u8]) -> (&str, &[u8]) { + if buf.is_empty() { + return ("", &[]); + } + let len = buf[0] as usize; + let str = std::str::from_utf8(&buf[1..len + 1]).expect("attr should be ascii or utf-8"); + let remaining_buf = &buf[len + 1..]; + (str, remaining_buf) +} From 6c9f139c1d786741a490b9ab7cd3bcccb6828ca0 Mon Sep 17 00:00:00 2001 From: Joe Howarth Date: Fri, 4 Feb 2022 14:29:19 -0500 Subject: [PATCH 2/2] remove copy-pasta comment --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e747120..1c46959 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -411,8 +411,6 @@ impl<'a> Iterator for AttributeIter<'a> { return Some((key, val)); } } -// example usage of pyth-client account structure -// bootstrap all product and pricing accounts from root mapping account fn get_attr_str(buf: &[u8]) -> (&str, &[u8]) { if buf.is_empty() {