From 6eb38d8fa4eb1bf8be22bfff05bcc4c9b8625034 Mon Sep 17 00:00:00 2001 From: c Date: Mon, 16 Sep 2024 22:51:47 +0200 Subject: [PATCH] refactor: lift cloning to caller --- bindings/python/src/normalizers.rs | 2 +- bindings/python/src/pre_tokenizers.rs | 2 +- bindings/python/src/utils/mod.rs | 10 +++++----- bindings/python/src/utils/pretokenization.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bindings/python/src/normalizers.rs b/bindings/python/src/normalizers.rs index e5b5cb324..feff9811c 100644 --- a/bindings/python/src/normalizers.rs +++ b/bindings/python/src/normalizers.rs @@ -553,7 +553,7 @@ impl tk::tokenizer::Normalizer for CustomNormalizer { Python::with_gil(|py| { let normalized = PyNormalizedStringRefMut::new(normalized); let py_normalized = self.inner.bind(py); - py_normalized.call_method("normalize", (normalized.get(),), None)?; + py_normalized.call_method("normalize", (normalized.get().clone(),), None)?; Ok(()) }) } diff --git a/bindings/python/src/pre_tokenizers.rs b/bindings/python/src/pre_tokenizers.rs index bac3284ad..1c43f7eb8 100644 --- a/bindings/python/src/pre_tokenizers.rs +++ b/bindings/python/src/pre_tokenizers.rs @@ -634,7 +634,7 @@ impl tk::tokenizer::PreTokenizer for CustomPreTokenizer { Python::with_gil(|py| { let pretok = PyPreTokenizedStringRefMut::new(sentence); let py_pretok = self.inner.bind(py); - py_pretok.call_method("pre_tokenize", (pretok.get(),), None)?; + py_pretok.call_method("pre_tokenize", (pretok.get().clone(),), None)?; Ok(()) }) } diff --git a/bindings/python/src/utils/mod.rs b/bindings/python/src/utils/mod.rs index 43352a7fa..21b3fc1e1 100644 --- a/bindings/python/src/utils/mod.rs +++ b/bindings/python/src/utils/mod.rs @@ -18,11 +18,11 @@ pub trait DestroyPtr { fn destroy(&mut self); } -pub struct RefMutGuard<'r, T: DestroyPtr + Clone> { +pub struct RefMutGuard<'r, T: DestroyPtr> { content: T, r: PhantomData<&'r mut T>, } -impl RefMutGuard<'_, T> { +impl RefMutGuard<'_, T> { pub fn new(content: T) -> Self { Self { content, @@ -30,12 +30,12 @@ impl RefMutGuard<'_, T> { } } - pub fn get(&self) -> T { - self.content.clone() + pub fn get(&self) -> &T { + &self.content } } -impl Drop for RefMutGuard<'_, T> { +impl Drop for RefMutGuard<'_, T> { fn drop(&mut self) { self.content.destroy() } diff --git a/bindings/python/src/utils/pretokenization.rs b/bindings/python/src/utils/pretokenization.rs index 88fdd19f5..021a8ec90 100644 --- a/bindings/python/src/utils/pretokenization.rs +++ b/bindings/python/src/utils/pretokenization.rs @@ -39,7 +39,7 @@ fn normalize(pretok: &mut PreTokenizedString, func: &Bound<'_, PyAny>) -> PyResu } else { ToPyResult(pretok.normalize(|normalized| { let norm = PyNormalizedStringRefMut::new(normalized); - func.call((norm.get(),), None)?; + func.call((norm.get().clone(),), None)?; Ok(()) })) .into()