11use crate :: bytecode:: * ;
22use crate :: marshal:: { self , Read , ReadBorrowed , Write } ;
33
4- /// A frozen module. Holds a frozen code object and whether it is part of a package
4+ /// A frozen module. Holds a frozen code object and whether it is part of a package.
5+ /// The `origname` type is generic to allow either static names (runtime) or
6+ /// borrowed/owned names during compile-time freezing.
57#[ derive( Copy , Clone ) ]
6- pub struct FrozenModule < B = & ' static [ u8 ] > {
8+ pub struct FrozenModule < B = & ' static [ u8 ] , N = & ' static str > {
79 pub code : FrozenCodeObject < B > ,
810 pub package : bool ,
11+ pub origname : N ,
912}
1013
1114#[ derive( Copy , Clone ) ]
@@ -55,7 +58,7 @@ impl<B: AsRef<[u8]> + ?Sized> FrozenLib<B> {
5558}
5659
5760impl < ' a , B : AsRef < [ u8 ] > + ?Sized > IntoIterator for & ' a FrozenLib < B > {
58- type Item = ( & ' a str , FrozenModule < & ' a [ u8 ] > ) ;
61+ type Item = ( & ' a str , FrozenModule < & ' a [ u8 ] , & ' a str > ) ;
5962 type IntoIter = FrozenModulesIter < ' a > ;
6063
6164 fn into_iter ( self ) -> Self :: IntoIter {
@@ -69,7 +72,7 @@ pub struct FrozenModulesIter<'a> {
6972}
7073
7174impl < ' a > Iterator for FrozenModulesIter < ' a > {
72- type Item = ( & ' a str , FrozenModule < & ' a [ u8 ] > ) ;
75+ type Item = ( & ' a str , FrozenModule < & ' a [ u8 ] , & ' a str > ) ;
7376
7477 fn next ( & mut self ) -> Option < Self :: Item > {
7578 if self . remaining > 0 {
@@ -90,21 +93,30 @@ impl ExactSizeIterator for FrozenModulesIter<'_> {}
9093
9194fn read_entry < ' a > (
9295 rdr : & mut & ' a [ u8 ] ,
93- ) -> Result < ( & ' a str , FrozenModule < & ' a [ u8 ] > ) , marshal:: MarshalError > {
96+ ) -> Result < ( & ' a str , FrozenModule < & ' a [ u8 ] , & ' a str > ) , marshal:: MarshalError > {
9497 let len = rdr. read_u32 ( ) ?;
9598 let name = rdr. read_str_borrow ( len) ?;
9699 let len = rdr. read_u32 ( ) ?;
97100 let code_slice = rdr. read_slice_borrow ( len) ?;
98101 let code = FrozenCodeObject { bytes : code_slice } ;
99102 let package = rdr. read_u8 ( ) ? != 0 ;
100- Ok ( ( name, FrozenModule { code, package } ) )
103+ let len = rdr. read_u32 ( ) ?;
104+ let origname = rdr. read_str_borrow ( len) ?;
105+ Ok ( (
106+ name,
107+ FrozenModule {
108+ code,
109+ package,
110+ origname,
111+ } ,
112+ ) )
101113}
102114
103115impl FrozenLib < Vec < u8 > > {
104116 /// Encode the given iterator of frozen modules into a compressed vector of bytes
105- pub fn encode < ' a , I , B : AsRef < [ u8 ] > > ( lib : I ) -> Self
117+ pub fn encode < ' a , I , B : AsRef < [ u8 ] > , N : AsRef < str > > ( lib : I ) -> Self
106118 where
107- I : IntoIterator < Item = ( & ' a str , FrozenModule < B > ) , IntoIter : ExactSizeIterator + Clone > ,
119+ I : IntoIterator < Item = ( & ' a str , FrozenModule < B , N > ) , IntoIter : ExactSizeIterator + Clone > ,
108120 {
109121 let iter = lib. into_iter ( ) ;
110122 let mut bytes = Vec :: new ( ) ;
@@ -113,18 +125,23 @@ impl FrozenLib<Vec<u8>> {
113125 }
114126}
115127
116- fn write_lib < ' a , B : AsRef < [ u8 ] > > (
128+ fn write_lib < ' a , B : AsRef < [ u8 ] > , N : AsRef < str > > (
117129 buf : & mut Vec < u8 > ,
118- lib : impl ExactSizeIterator < Item = ( & ' a str , FrozenModule < B > ) > ,
130+ lib : impl ExactSizeIterator < Item = ( & ' a str , FrozenModule < B , N > ) > ,
119131) {
120132 marshal:: write_len ( buf, lib. len ( ) ) ;
121133 for ( name, module) in lib {
122134 write_entry ( buf, name, module) ;
123135 }
124136}
125137
126- fn write_entry ( buf : & mut Vec < u8 > , name : & str , module : FrozenModule < impl AsRef < [ u8 ] > > ) {
138+ fn write_entry < N : AsRef < str > > (
139+ buf : & mut Vec < u8 > ,
140+ name : & str ,
141+ module : FrozenModule < impl AsRef < [ u8 ] > , N > ,
142+ ) {
127143 marshal:: write_vec ( buf, name. as_bytes ( ) ) ;
128144 marshal:: write_vec ( buf, module. code . bytes . as_ref ( ) ) ;
129145 buf. write_u8 ( module. package as u8 ) ;
146+ marshal:: write_vec ( buf, module. origname . as_ref ( ) . as_bytes ( ) ) ;
130147}
0 commit comments