@@ -2,7 +2,6 @@ use std::borrow::Cow;
2
2
use std:: collections:: HashMap ;
3
3
use std:: ffi:: CString ;
4
4
use std:: io:: Result as IoResult ;
5
- use std:: marker:: PhantomData ;
6
5
use std:: panic:: Location ;
7
6
use std:: path:: { Path , PathBuf } ;
8
7
use std:: string:: String as StdString ;
@@ -17,7 +16,7 @@ use crate::value::Value;
17
16
/// Trait for types [loadable by Lua] and convertible to a [`Chunk`]
18
17
///
19
18
/// [loadable by Lua]: https://www.lua.org/manual/5.4/manual.html#3.3.2
20
- pub trait AsChunk < ' a > {
19
+ pub trait AsChunk {
21
20
/// Returns optional chunk name
22
21
///
23
22
/// See [`Chunk::set_name`] for possible name prefixes.
@@ -39,61 +38,75 @@ pub trait AsChunk<'a> {
39
38
}
40
39
41
40
/// Returns chunk data (can be text or binary)
42
- fn source ( self ) -> IoResult < Cow < ' a , [ u8 ] > > ;
41
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > >
42
+ where
43
+ Self : ' a ;
43
44
}
44
45
45
- impl < ' a > AsChunk < ' a > for & ' a str {
46
- fn source ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
46
+ impl AsChunk for & str {
47
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > >
48
+ where
49
+ Self : ' a ,
50
+ {
47
51
Ok ( Cow :: Borrowed ( self . as_ref ( ) ) )
48
52
}
49
53
}
50
54
51
- impl AsChunk < ' static > for StdString {
52
- fn source ( self ) -> IoResult < Cow < ' static , [ u8 ] > > {
55
+ impl AsChunk for StdString {
56
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
53
57
Ok ( Cow :: Owned ( self . into_bytes ( ) ) )
54
58
}
55
59
}
56
60
57
- impl < ' a > AsChunk < ' a > for & ' a StdString {
58
- fn source ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
61
+ impl AsChunk for & StdString {
62
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > >
63
+ where
64
+ Self : ' a ,
65
+ {
59
66
Ok ( Cow :: Borrowed ( self . as_bytes ( ) ) )
60
67
}
61
68
}
62
69
63
- impl < ' a > AsChunk < ' a > for & ' a [ u8 ] {
64
- fn source ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
70
+ impl AsChunk for & [ u8 ] {
71
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > >
72
+ where
73
+ Self : ' a ,
74
+ {
65
75
Ok ( Cow :: Borrowed ( self ) )
66
76
}
67
77
}
68
78
69
- impl AsChunk < ' static > for Vec < u8 > {
70
- fn source ( self ) -> IoResult < Cow < ' static , [ u8 ] > > {
79
+ impl AsChunk for Vec < u8 > {
80
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
71
81
Ok ( Cow :: Owned ( self ) )
72
82
}
73
83
}
74
84
75
- impl < ' a > AsChunk < ' a > for & ' a Vec < u8 > {
76
- fn source ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
77
- Ok ( Cow :: Borrowed ( self . as_ref ( ) ) )
85
+ impl AsChunk for & Vec < u8 > {
86
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > >
87
+ where
88
+ Self : ' a ,
89
+ {
90
+ Ok ( Cow :: Borrowed ( self ) )
78
91
}
79
92
}
80
93
81
- impl AsChunk < ' static > for & Path {
94
+ impl AsChunk for & Path {
82
95
fn name ( & self ) -> Option < StdString > {
83
96
Some ( format ! ( "@{}" , self . display( ) ) )
84
97
}
85
98
86
- fn source ( self ) -> IoResult < Cow < ' static , [ u8 ] > > {
99
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
87
100
std:: fs:: read ( self ) . map ( Cow :: Owned )
88
101
}
89
102
}
90
103
91
- impl AsChunk < ' static > for PathBuf {
104
+ impl AsChunk for PathBuf {
92
105
fn name ( & self ) -> Option < StdString > {
93
106
Some ( format ! ( "@{}" , self . display( ) ) )
94
107
}
95
108
96
- fn source ( self ) -> IoResult < Cow < ' static , [ u8 ] > > {
109
+ fn source < ' a > ( self ) -> IoResult < Cow < ' a , [ u8 ] > > {
97
110
std:: fs:: read ( self ) . map ( Cow :: Owned )
98
111
}
99
112
}
@@ -506,10 +519,10 @@ impl Chunk<'_> {
506
519
if self . detect_mode ( ) == ChunkMode :: Binary {
507
520
let lua = self . lua . lock ( ) ;
508
521
if let Some ( mut cache) = lua. app_data_mut_unguarded :: < ChunksCache > ( ) {
509
- cache. 0 . insert ( text_source, binary_source. as_ref ( ) . to_vec ( ) ) ;
522
+ cache. 0 . insert ( text_source, binary_source. to_vec ( ) ) ;
510
523
} else {
511
524
let mut cache = ChunksCache ( HashMap :: new ( ) ) ;
512
- cache. 0 . insert ( text_source, binary_source. as_ref ( ) . to_vec ( ) ) ;
525
+ cache. 0 . insert ( text_source, binary_source. to_vec ( ) ) ;
513
526
let _ = lua. try_set_app_data ( cache) ;
514
527
} ;
515
528
}
@@ -543,21 +556,20 @@ impl Chunk<'_> {
543
556
}
544
557
545
558
fn detect_mode ( & self ) -> ChunkMode {
546
- match ( self . mode , & self . source ) {
547
- ( Some ( mode) , _) => mode,
548
- ( None , Ok ( source) ) => {
549
- #[ cfg( not( feature = "luau" ) ) ]
550
- if source. starts_with ( ffi:: LUA_SIGNATURE ) {
551
- return ChunkMode :: Binary ;
552
- }
553
- #[ cfg( feature = "luau" ) ]
554
- if * source. first ( ) . unwrap_or ( & u8:: MAX ) < b'\n' {
555
- return ChunkMode :: Binary ;
556
- }
557
- ChunkMode :: Text
559
+ if let Some ( mode) = self . mode {
560
+ return mode;
561
+ }
562
+ if let Ok ( source) = & self . source {
563
+ #[ cfg( not( feature = "luau" ) ) ]
564
+ if source. starts_with ( ffi:: LUA_SIGNATURE ) {
565
+ return ChunkMode :: Binary ;
566
+ }
567
+ #[ cfg( feature = "luau" ) ]
568
+ if * source. first ( ) . unwrap_or ( & u8:: MAX ) < b'\n' {
569
+ return ChunkMode :: Binary ;
558
570
}
559
- ( None , Err ( _) ) => ChunkMode :: Text , // any value is fine
560
571
}
572
+ ChunkMode :: Text
561
573
}
562
574
563
575
fn convert_name ( name : String ) -> Result < CString > {
@@ -572,29 +584,27 @@ impl Chunk<'_> {
572
584
}
573
585
}
574
586
575
- struct WrappedChunk < ' a , T : AsChunk < ' a > > {
587
+ struct WrappedChunk < T : AsChunk > {
576
588
chunk : T ,
577
589
caller : & ' static Location < ' static > ,
578
- _marker : PhantomData < & ' a T > ,
579
590
}
580
591
581
- impl < ' a > Chunk < ' a > {
592
+ impl Chunk < ' _ > {
582
593
/// Wraps a chunk of Lua code, returning an opaque type that implements [`IntoLua`] trait.
583
594
///
584
595
/// The resulted `IntoLua` implementation will convert the chunk into a Lua function without
585
596
/// executing it.
586
597
#[ doc( hidden) ]
587
598
#[ track_caller]
588
- pub fn wrap ( chunk : impl AsChunk < ' a > + ' a ) -> impl IntoLua + ' a {
599
+ pub fn wrap ( chunk : impl AsChunk ) -> impl IntoLua {
589
600
WrappedChunk {
590
601
chunk,
591
602
caller : Location :: caller ( ) ,
592
- _marker : PhantomData ,
593
603
}
594
604
}
595
605
}
596
606
597
- impl < ' a , T : AsChunk < ' a > > IntoLua for WrappedChunk < ' a , T > {
607
+ impl < T : AsChunk > IntoLua for WrappedChunk < T > {
598
608
fn into_lua ( self , lua : & Lua ) -> Result < Value > {
599
609
lua. load_with_location ( self . chunk , self . caller )
600
610
. into_function ( )
0 commit comments