Skip to content

Commit 2e7c654

Browse files
committed
Make AsChunk trait dyn-friendly
1 parent f36aaa5 commit 2e7c654

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

src/chunk.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@ pub trait AsChunk {
3838
}
3939

4040
/// Returns chunk data (can be text or binary)
41-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>>
41+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>>
4242
where
4343
Self: 'a;
4444
}
4545

4646
impl AsChunk for &str {
47-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>>
47+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>>
4848
where
4949
Self: 'a,
5050
{
51-
Ok(Cow::Borrowed(self.as_ref()))
51+
Ok(Cow::Borrowed(self.as_bytes()))
5252
}
5353
}
5454

5555
impl AsChunk for StdString {
56-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
57-
Ok(Cow::Owned(self.into_bytes()))
56+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>> {
57+
Ok(Cow::Owned(self.clone().into_bytes()))
5858
}
5959
}
6060

6161
impl AsChunk for &StdString {
62-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>>
62+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>>
6363
where
6464
Self: 'a,
6565
{
@@ -68,7 +68,7 @@ impl AsChunk for &StdString {
6868
}
6969

7070
impl AsChunk for &[u8] {
71-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>>
71+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>>
7272
where
7373
Self: 'a,
7474
{
@@ -77,13 +77,13 @@ impl AsChunk for &[u8] {
7777
}
7878

7979
impl AsChunk for Vec<u8> {
80-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
81-
Ok(Cow::Owned(self))
80+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>> {
81+
Ok(Cow::Owned(self.clone()))
8282
}
8383
}
8484

8585
impl AsChunk for &Vec<u8> {
86-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>>
86+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>>
8787
where
8888
Self: 'a,
8989
{
@@ -96,7 +96,7 @@ impl AsChunk for &Path {
9696
Some(format!("@{}", self.display()))
9797
}
9898

99-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
99+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>> {
100100
std::fs::read(self).map(Cow::Owned)
101101
}
102102
}
@@ -106,11 +106,32 @@ impl AsChunk for PathBuf {
106106
Some(format!("@{}", self.display()))
107107
}
108108

109-
fn source<'a>(self) -> IoResult<Cow<'a, [u8]>> {
109+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>> {
110110
std::fs::read(self).map(Cow::Owned)
111111
}
112112
}
113113

114+
impl<C: AsChunk + ?Sized> AsChunk for Box<C> {
115+
fn name(&self) -> Option<StdString> {
116+
(**self).name()
117+
}
118+
119+
fn environment(&self, lua: &Lua) -> Result<Option<Table>> {
120+
(**self).environment(lua)
121+
}
122+
123+
fn mode(&self) -> Option<ChunkMode> {
124+
(**self).mode()
125+
}
126+
127+
fn source<'a>(&self) -> IoResult<Cow<'a, [u8]>>
128+
where
129+
Self: 'a,
130+
{
131+
(**self).source()
132+
}
133+
}
134+
114135
/// Returned from [`Lua::load`] and is used to finalize loading and executing Lua main chunks.
115136
#[must_use = "`Chunk`s do nothing unless one of `exec`, `eval`, `call`, or `into_function` are called on them"]
116137
pub struct Chunk<'a> {

0 commit comments

Comments
 (0)