From 5d82c7659be2c278c29771ece41d9ab2023c1516 Mon Sep 17 00:00:00 2001 From: yukirij Date: Sat, 19 Aug 2023 00:40:03 -0700 Subject: [PATCH] Fix decode bug in varying. --- README.md | 4 ++-- src/encoding/mod.rs | 12 +++++++++++- src/interface/builder.rs | 4 ++-- src/interface/varying.rs | 7 +++++++ src/lib.rs | 33 +++++---------------------------- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index b82848f..fb3c141 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ let refer = acquire(list(integer())); `release(refer:Reference)` -Destruct and deallocate a type. +Destruct and deallocate an object. ``` release(refer); ``` @@ -173,7 +173,7 @@ println!("{}", target.get()); // prints "Hello, world!" `copy(dst:Reference, src:Reference) -> Result<(),()>` -Copy the contents of an objcet to another location, keeping the original. +Copy the contents of an object to another location, keeping the original. ``` let original = Sequence::with("Hello, world!"); diff --git a/src/encoding/mod.rs b/src/encoding/mod.rs index 78eb740..e0ce6f3 100644 --- a/src/encoding/mod.rs +++ b/src/encoding/mod.rs @@ -10,6 +10,7 @@ use crate::{ }; use crate::kind; use crate::{ + Varying, Boolean, Natural, Integer, Block, Sequence, @@ -39,6 +40,10 @@ pub fn encode_data(addr:Reference) -> Vec { let mut result = Vec::::new(); match unsafe {type_key(addr.class)} { + tag::VARYING => { + let data = Varying::from(addr).unwrap(); + result.append(&mut encode(data.get())); + } tag::BOOLEAN => { result.append(&mut util::pack_natural(Boolean::from(addr).unwrap().get() as u64)); } @@ -128,7 +133,12 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { - return decode(data, index); + return match decode(data, index) { + Ok(value) => { + Ok(Type::Varying(Varying::with(value.get()))) + } + Err(_) => Err(()), + } } tag::BOOLEAN => { return Ok(Type::Boolean(Boolean::with(unpack_natural(data, index) == 1))); diff --git a/src/interface/builder.rs b/src/interface/builder.rs index be89c7e..431dea4 100644 --- a/src/interface/builder.rs +++ b/src/interface/builder.rs @@ -4,8 +4,8 @@ use crate::runtime; pub fn inner(type_id:usize) -> usize { unsafe {runtime::type_inner(type_id)} } pub fn kind(type_id:usize) -> usize { unsafe {runtime::type_key(type_id)} } -pub fn varying() -> usize { 0 } -pub fn null() -> usize { unsafe { runtime::type_outer(0, tag::NULL) } } +pub fn null() -> usize { 0 } +pub fn varying() -> usize { unsafe { runtime::type_outer(0, tag::VARYING) } } pub fn boolean() -> usize { unsafe { runtime::type_outer(0, tag::BOOLEAN) } } pub fn natural() -> usize { unsafe { runtime::type_outer(0, tag::NATURAL) } } pub fn integer() -> usize { unsafe { runtime::type_outer(0, tag::INTEGER) } } diff --git a/src/interface/varying.rs b/src/interface/varying.rs index 427a77b..91ff12d 100644 --- a/src/interface/varying.rs +++ b/src/interface/varying.rs @@ -31,6 +31,13 @@ impl Varying { } } + pub fn with(addr:Reference) -> Self + { + let mut obj = Varying::new(); + obj.set(addr); + return obj; + } + pub fn detach(&mut self) { self.managed = false; diff --git a/src/lib.rs b/src/lib.rs index a148aec..f111045 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,28 +7,10 @@ mod interface; pub use interface::*; mod encoding; pub use encoding::*; pub fn test() { - Schema::with(vec![ - ("nat", natural()), - ("pass", block(6)), - ]).bind(9); - Schema::with(vec![ - ("str", sequence()), - ("int", integer()), - ("bool", boolean()), - ("rec", record(9)), - ]).bind(10); + let mut data = Varying::new(); + data.set(*Natural::with(79)); - let rec = Record::with(10, vec![ - ("str", *Sequence::with("hello!")), - ("int", *Integer::with(-70)), - ("bool", *Boolean::with(true)), - ("rec", *Record::with(9, vec![ - ("nat", *Natural::with(8)), - ("pass", *Block::with(6, vec![1, 2, 3, 4, 5, 6])), - ]).unwrap()), - ]).unwrap(); - - let out = encode(*rec); + let out = encode(*data); print!("[{}]: ", out.len()); for byte in &out { print!("{:02x} ", *byte); @@ -36,13 +18,8 @@ pub fn test() { match decode(&out, &mut 0) { Ok(ty) => match ty { - Type::Record(rec) => { - println!("{}", Sequence::from(rec.get("str")).unwrap().get()); - println!("{}", Integer::from(rec.get("int")).unwrap().get()); - println!("{}", Boolean::from(rec.get("bool")).unwrap().get()); - let sub = Record::from(rec.get("rec")).unwrap(); - println!("{}", Natural::from(sub.get("nat")).unwrap().get()); - println!("{:?}", Block::from(sub.get("pass")).unwrap().get()); + Type::Varying(data) => { + println!("Ok: {}", Natural::from(data.get()).unwrap().get()); } _ => { println!("Other"); } },