Skip to content

Commit 58965c6

Browse files
committedJan 28, 2025··
Move lifetime from AsChunk<'a> to AsChunk::source
1 parent 8574682 commit 58965c6

File tree

3 files changed

+55
-45
lines changed

3 files changed

+55
-45
lines changed
 

‎mlua_derive/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn chunk(input: TokenStream) -> TokenStream {
103103

104104
struct InnerChunk<F: FnOnce(&Lua) -> Result<Table>>(Cell<Option<F>>);
105105

106-
impl<F> AsChunk<'static> for InnerChunk<F>
106+
impl<F> AsChunk for InnerChunk<F>
107107
where
108108
F: FnOnce(&Lua) -> Result<Table>,
109109
{
@@ -120,7 +120,7 @@ pub fn chunk(input: TokenStream) -> TokenStream {
120120
Some(ChunkMode::Text)
121121
}
122122

123-
fn source(self) -> IoResult<Cow<'static, [u8]>> {
123+
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
124124
Ok(Cow::Borrowed((#source).as_bytes()))
125125
}
126126
}

‎src/chunk.rs

+51-41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::ffi::CString;
44
use std::io::Result as IoResult;
5-
use std::marker::PhantomData;
65
use std::panic::Location;
76
use std::path::{Path, PathBuf};
87
use std::string::String as StdString;
@@ -17,7 +16,7 @@ use crate::value::Value;
1716
/// Trait for types [loadable by Lua] and convertible to a [`Chunk`]
1817
///
1918
/// [loadable by Lua]: https://www.lua.org/manual/5.4/manual.html#3.3.2
20-
pub trait AsChunk<'a> {
19+
pub trait AsChunk {
2120
/// Returns optional chunk name
2221
///
2322
/// See [`Chunk::set_name`] for possible name prefixes.
@@ -39,61 +38,75 @@ pub trait AsChunk<'a> {
3938
}
4039

4140
/// 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;
4344
}
4445

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+
{
4751
Ok(Cow::Borrowed(self.as_ref()))
4852
}
4953
}
5054

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]>> {
5357
Ok(Cow::Owned(self.into_bytes()))
5458
}
5559
}
5660

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+
{
5966
Ok(Cow::Borrowed(self.as_bytes()))
6067
}
6168
}
6269

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+
{
6575
Ok(Cow::Borrowed(self))
6676
}
6777
}
6878

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]>> {
7181
Ok(Cow::Owned(self))
7282
}
7383
}
7484

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))
7891
}
7992
}
8093

81-
impl AsChunk<'static> for &Path {
94+
impl AsChunk for &Path {
8295
fn name(&self) -> Option<StdString> {
8396
Some(format!("@{}", self.display()))
8497
}
8598

86-
fn source(self) -> IoResult<Cow<'static, [u8]>> {
99+
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
87100
std::fs::read(self).map(Cow::Owned)
88101
}
89102
}
90103

91-
impl AsChunk<'static> for PathBuf {
104+
impl AsChunk for PathBuf {
92105
fn name(&self) -> Option<StdString> {
93106
Some(format!("@{}", self.display()))
94107
}
95108

96-
fn source(self) -> IoResult<Cow<'static, [u8]>> {
109+
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
97110
std::fs::read(self).map(Cow::Owned)
98111
}
99112
}
@@ -506,10 +519,10 @@ impl Chunk<'_> {
506519
if self.detect_mode() == ChunkMode::Binary {
507520
let lua = self.lua.lock();
508521
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());
510523
} else {
511524
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());
513526
let _ = lua.try_set_app_data(cache);
514527
};
515528
}
@@ -543,21 +556,20 @@ impl Chunk<'_> {
543556
}
544557

545558
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;
558570
}
559-
(None, Err(_)) => ChunkMode::Text, // any value is fine
560571
}
572+
ChunkMode::Text
561573
}
562574

563575
fn convert_name(name: String) -> Result<CString> {
@@ -572,29 +584,27 @@ impl Chunk<'_> {
572584
}
573585
}
574586

575-
struct WrappedChunk<'a, T: AsChunk<'a>> {
587+
struct WrappedChunk<T: AsChunk> {
576588
chunk: T,
577589
caller: &'static Location<'static>,
578-
_marker: PhantomData<&'a T>,
579590
}
580591

581-
impl<'a> Chunk<'a> {
592+
impl Chunk<'_> {
582593
/// Wraps a chunk of Lua code, returning an opaque type that implements [`IntoLua`] trait.
583594
///
584595
/// The resulted `IntoLua` implementation will convert the chunk into a Lua function without
585596
/// executing it.
586597
#[doc(hidden)]
587598
#[track_caller]
588-
pub fn wrap(chunk: impl AsChunk<'a> + 'a) -> impl IntoLua + 'a {
599+
pub fn wrap(chunk: impl AsChunk) -> impl IntoLua {
589600
WrappedChunk {
590601
chunk,
591602
caller: Location::caller(),
592-
_marker: PhantomData,
593603
}
594604
}
595605
}
596606

597-
impl<'a, T: AsChunk<'a>> IntoLua for WrappedChunk<'a, T> {
607+
impl<T: AsChunk> IntoLua for WrappedChunk<T> {
598608
fn into_lua(self, lua: &Lua) -> Result<Value> {
599609
lua.load_with_location(self.chunk, self.caller)
600610
.into_function()

‎src/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -999,13 +999,13 @@ impl Lua {
999999
///
10001000
/// [`Chunk::exec`]: crate::Chunk::exec
10011001
#[track_caller]
1002-
pub fn load<'a>(&self, chunk: impl AsChunk<'a>) -> Chunk<'a> {
1002+
pub fn load<'a>(&self, chunk: impl AsChunk + 'a) -> Chunk<'a> {
10031003
self.load_with_location(chunk, Location::caller())
10041004
}
10051005

10061006
pub(crate) fn load_with_location<'a>(
10071007
&self,
1008-
chunk: impl AsChunk<'a>,
1008+
chunk: impl AsChunk + 'a,
10091009
location: &'static Location<'static>,
10101010
) -> Chunk<'a> {
10111011
Chunk {

0 commit comments

Comments
 (0)
Please sign in to comment.