MCPcopy Create free account
hub / github.com/cryptape/rust-numext / convert_into

Method convert_into

constructor/src/fixed_uint/core/internal/kernel.rs:24–94  ·  view source on GitHub ↗
(&self, uc: &Self)

Source from the content-addressed store, hash-verified

22 }
23
24 pub fn convert_into(&self, uc: &Self) -> TokenStream {
25 let this_name = &self.ts.name;
26 let this_feature = &self.ts.feature;
27 let that_name = &uc.ts.name;
28 let that_feature = &uc.ts.feature;
29 let stmts = match self.info.bits_size.cmp(&uc.info.bits_size) {
30 cmp::Ordering::Equal => {
31 if self.info.unit_bits_size == uc.info.unit_bits_size {
32 // same inner
33 quote!(
34 let inner = self.inner();
35 let val = #that_name::new(inner.clone());
36 (val, false)
37 )
38 } else if uc.info.unit_bits_size == 8 {
39 // into u8 array
40 let that_unit_amount = &uc.ts.unit_amount;
41 quote!(
42 let mut inner = [0u8; #that_unit_amount];
43 self.into_little_endian(&mut inner[..]).unwrap();
44 let val = #that_name::new(inner);
45 (val, false)
46 )
47 } else if self.info.unit_bits_size == 8 {
48 // from u8 array
49 quote!(
50 let inner = self.inner();
51 let val = #that_name::from_little_endian(&inner[..]).unwrap();
52 (val, false)
53 )
54 } else {
55 // same size, diff inner, no u8 array
56 let that_bytes_size = &uc.ts.bytes_size;
57 quote!(
58 let mut tmp = [0u8; #that_bytes_size];
59 self.into_little_endian(&mut tmp[..]).unwrap();
60 let val = #that_name::from_little_endian(&tmp[..]).unwrap();
61 (val, false)
62 )
63 }
64 }
65 cmp::Ordering::Less => {
66 let this_bytes_size = &self.ts.bytes_size;
67 quote!(
68 let mut tmp = [0u8; #this_bytes_size];
69 self.into_little_endian(&mut tmp[..]).unwrap();
70 let val = #that_name::from_little_endian(&tmp[..]).unwrap();
71 (val, false)
72 )
73 }
74 cmp::Ordering::Greater => {
75 let this_bytes_size = &self.ts.bytes_size;
76 let that_bytes_size = &uc.ts.bytes_size;
77 quote!(
78 let mut tmp = [0u8; #this_bytes_size];
79 self.into_little_endian(&mut tmp[..]).unwrap();
80 let val = #that_name::from_little_endian(&tmp[..#that_bytes_size]).unwrap();
81 (val, true)

Callers 2

outputMethod · 0.45
convertFunction · 0.45

Calls

no outgoing calls

Tested by 1

convertFunction · 0.36