@@ -2,14 +2,17 @@ use std::borrow::Cow;
22use std:: collections:: HashMap ;
33use std:: ffi:: CString ;
44use std:: io:: Result as IoResult ;
5+ use std:: marker:: PhantomData ;
6+ use std:: panic:: Location ;
57use std:: path:: { Path , PathBuf } ;
68use std:: string:: String as StdString ;
79
810use crate :: error:: { Error , Result } ;
911use crate :: function:: Function ;
1012use crate :: state:: { Lua , WeakLua } ;
1113use crate :: table:: Table ;
12- use crate :: traits:: { FromLuaMulti , IntoLuaMulti } ;
14+ use crate :: traits:: { FromLuaMulti , IntoLua , IntoLuaMulti } ;
15+ use crate :: value:: Value ;
1316
1417/// Trait for types [loadable by Lua] and convertible to a [`Chunk`]
1518///
@@ -561,3 +564,32 @@ impl Chunk<'_> {
561564 buf
562565 }
563566}
567+
568+ struct WrappedChunk < ' a , T : AsChunk < ' a > > {
569+ chunk : T ,
570+ caller : & ' static Location < ' static > ,
571+ _marker : PhantomData < & ' a T > ,
572+ }
573+
574+ impl < ' a > Chunk < ' a > {
575+ /// Wraps a chunk of Lua code, returning an opaque type that implements [`IntoLua`] trait.
576+ ///
577+ /// The resulted `IntoLua` implementation will convert the chunk into a Lua function without
578+ /// executing it.
579+ #[ track_caller]
580+ pub fn wrap ( chunk : impl AsChunk < ' a > + ' a ) -> impl IntoLua + ' a {
581+ WrappedChunk {
582+ chunk,
583+ caller : Location :: caller ( ) ,
584+ _marker : PhantomData ,
585+ }
586+ }
587+ }
588+
589+ impl < ' a , T : AsChunk < ' a > > IntoLua for WrappedChunk < ' a , T > {
590+ fn into_lua ( self , lua : & Lua ) -> Result < Value > {
591+ lua. load_with_location ( self . chunk , self . caller )
592+ . into_function ( )
593+ . map ( Value :: Function )
594+ }
595+ }
0 commit comments