Fix decode bug in varying.
This commit is contained in:
parent
24ce88cb3f
commit
5d82c7659b
@ -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!");
|
||||
|
@ -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<u8>
|
||||
{
|
||||
let mut result = Vec::<u8>::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<u8>, type_id:usize, index:&mut usize) -> Result<Typ
|
||||
{
|
||||
match unsafe {type_key(type_id)} {
|
||||
tag::VARYING => {
|
||||
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)));
|
||||
|
@ -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) } }
|
||||
|
@ -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;
|
||||
|
33
src/lib.rs
33
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"); }
|
||||
},
|
||||
|
Reference in New Issue
Block a user