Skip to content

Commit 1574d44

Browse files
authored
chore(Rust): Remove new_from_fory from context.rs (#2831)
## What does this PR do? 1. Remove new_from_fory from context.rs 2. add `allow(clippy::needless_lifetimes)]` to keep lifetime but avoid lint error. 3. refactor some unit tests. ## Related issues close #2827
1 parent 26a67c0 commit 1574d44

File tree

8 files changed

+253
-327
lines changed

8 files changed

+253
-327
lines changed

java/fory-core/src/test/java/org/apache/fory/RustXlangTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,14 @@ private void _testStringSerializer(
316316
"Hello, 世界",
317317
};
318318
for (String s : testStrings) {
319-
serializer.writeJavaString(buffer, s);
319+
fory.serialize(buffer, s);
320320
}
321321
Pair<Map<String, String>, File> env_workdir =
322322
setFilePath(language, command, dataFile, buffer.getBytes(0, buffer.writerIndex()));
323323
Assert.assertTrue(executeCommand(command, 30, env_workdir.getLeft(), env_workdir.getRight()));
324324
buffer = MemoryUtils.wrap(Files.readAllBytes(dataFile));
325325
for (String expected : testStrings) {
326-
String actual = serializer.readJavaString(buffer);
326+
String actual = (String) fory.deserialize(buffer);
327327
Assert.assertEquals(actual, expected);
328328
}
329329
}
@@ -810,8 +810,9 @@ private void testConsistentNamed(Language language, List<String> command)
810810
for (int i = 0; i < 3; i++) {
811811
fory.serialize(buffer, Color.White);
812812
}
813-
// todo: checkVersion
814-
// fory.serialize(buffer, myStruct);
813+
for (int i = 0; i < 3; i++) {
814+
fory.serialize(buffer, myStruct);
815+
}
815816
for (int i = 0; i < 3; i++) {
816817
fory.serialize(buffer, myExt);
817818
}

rust/fory-core/src/buffer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,13 @@ impl<'a> Writer<'a> {
333333
}
334334

335335
#[derive(Default)]
336+
#[allow(clippy::needless_lifetimes)]
336337
pub struct Reader<'a> {
337338
pub(crate) bf: &'a [u8],
338339
pub(crate) cursor: usize,
339340
}
340341

342+
#[allow(clippy::needless_lifetimes)]
341343
impl<'a> Reader<'a> {
342344
#[inline(always)]
343345
pub fn new(bf: &[u8]) -> Reader<'_> {
@@ -743,5 +745,7 @@ impl<'a> Reader<'a> {
743745
}
744746
}
745747

748+
#[allow(clippy::needless_lifetimes)]
746749
unsafe impl<'a> Send for Reader<'a> {}
750+
#[allow(clippy::needless_lifetimes)]
747751
unsafe impl<'a> Sync for Reader<'a> {}

rust/fory-core/src/fory.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,6 @@ impl Fory {
335335
self.check_struct_version
336336
}
337337

338-
/// Returns a type resolver for type lookups.
339-
pub(crate) fn get_type_resolver(&self) -> &TypeResolver {
340-
&self.type_resolver
341-
}
342-
343338
/// Serializes a value of type `T` into a byte vector.
344339
///
345340
/// # Type Parameters

rust/fory-core/src/resolver/context.rs

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::buffer::{Reader, Writer};
1919
use std::mem;
2020

2121
use crate::error::Error;
22-
use crate::fory::Fory;
2322
use crate::meta::MetaString;
2423
use crate::resolver::meta_resolver::{MetaReaderResolver, MetaWriterResolver};
2524
use crate::resolver::meta_string_resolver::{MetaStringReaderResolver, MetaStringWriterResolver};
@@ -31,6 +30,7 @@ use std::rc::Rc;
3130

3231
/// Serialization state container used on a single thread at a time.
3332
/// Sharing the same instance across threads simultaneously causes undefined behavior.
33+
#[allow(clippy::needless_lifetimes)]
3434
pub struct WriteContext<'a> {
3535
// Replicated environment fields (direct access, no Arc indirection for flags)
3636
type_resolver: TypeResolver,
@@ -48,6 +48,7 @@ pub struct WriteContext<'a> {
4848
pub ref_writer: RefWriter,
4949
}
5050

51+
#[allow(clippy::needless_lifetimes)]
5152
impl<'a> WriteContext<'a> {
5253
pub fn new(
5354
type_resolver: TypeResolver,
@@ -89,27 +90,6 @@ impl<'a> WriteContext<'a> {
8990
self.writer = default.unwrap();
9091
}
9192

92-
/// Test method to create WriteContext from Fory instance
93-
/// Will be removed in future releases, do not use it in production code
94-
pub fn new_from_fory(fory: &Fory) -> WriteContext<'a> {
95-
WriteContext {
96-
default_writer: None,
97-
writer: Writer::from_buffer(Self::get_leak_buffer()),
98-
type_resolver: fory
99-
.get_type_resolver()
100-
.build_final_type_resolver()
101-
.unwrap(),
102-
compatible: fory.is_compatible(),
103-
share_meta: fory.is_share_meta(),
104-
compress_string: fory.is_compress_string(),
105-
xlang: fory.is_xlang(),
106-
check_struct_version: fory.is_check_struct_version(),
107-
meta_resolver: MetaWriterResolver::default(),
108-
meta_string_resolver: MetaStringWriterResolver::default(),
109-
ref_writer: RefWriter::new(),
110-
}
111-
}
112-
11393
/// Get type resolver
11494
#[inline(always)]
11595
pub fn get_type_resolver(&self) -> &TypeResolver {
@@ -227,6 +207,7 @@ impl<'a> WriteContext<'a> {
227207
}
228208
}
229209

210+
#[allow(clippy::needless_lifetimes)]
230211
impl<'a> Drop for WriteContext<'a> {
231212
fn drop(&mut self) {
232213
unsafe {
@@ -239,7 +220,9 @@ impl<'a> Drop for WriteContext<'a> {
239220
// ensures single-threaded access while the context is in use. Users must never hold the same
240221
// instance on multiple threads simultaneously; that would violate the invariants and result in
241222
// undefined behavior. Under that assumption, marking it Send/Sync is sound.
223+
#[allow(clippy::needless_lifetimes)]
242224
unsafe impl<'a> Send for WriteContext<'a> {}
225+
#[allow(clippy::needless_lifetimes)]
243226
unsafe impl<'a> Sync for WriteContext<'a> {}
244227

245228
/// Deserialization state container used on a single thread at a time.
@@ -265,7 +248,9 @@ pub struct ReadContext<'a> {
265248
// single-threaded use. Concurrent access to the same instance across threads is forbidden and
266249
// would result in undefined behavior. With exclusive use guaranteed, the Send/Sync markers are safe
267250
// even though Rc is used internally.
251+
#[allow(clippy::needless_lifetimes)]
268252
unsafe impl<'a> Send for ReadContext<'a> {}
253+
#[allow(clippy::needless_lifetimes)]
269254
unsafe impl<'a> Sync for ReadContext<'a> {}
270255

271256
impl<'a> ReadContext<'a> {
@@ -292,27 +277,6 @@ impl<'a> ReadContext<'a> {
292277
}
293278
}
294279

295-
/// Test method to create ReadContext from Fory instance
296-
/// Will be removed in future releases, do not use it in production code
297-
pub fn new_from_fory(fory: &Fory) -> ReadContext<'a> {
298-
ReadContext {
299-
type_resolver: fory
300-
.get_type_resolver()
301-
.build_final_type_resolver()
302-
.unwrap(),
303-
compatible: fory.is_compatible(),
304-
share_meta: fory.is_share_meta(),
305-
xlang: fory.is_xlang(),
306-
max_dyn_depth: fory.get_max_dyn_depth(),
307-
check_struct_version: fory.is_check_struct_version(),
308-
reader: Reader::default(),
309-
meta_resolver: MetaReaderResolver::default(),
310-
meta_string_resolver: MetaStringReaderResolver::default(),
311-
ref_reader: RefReader::new(),
312-
current_depth: 0,
313-
}
314-
}
315-
316280
/// Get type resolver
317281
#[inline(always)]
318282
pub fn get_type_resolver(&self) -> &TypeResolver {

rust/fory-core/src/row/row.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub struct ArrayGetter<'a, T> {
143143
_marker: PhantomData<T>,
144144
}
145145

146+
#[allow(clippy::needless_lifetimes)]
146147
impl<'a, T: Row<'a>> ArrayGetter<'a, T> {
147148
pub fn size(&self) -> usize {
148149
self.array_data.num_elements()
@@ -157,6 +158,7 @@ impl<'a, T: Row<'a>> ArrayGetter<'a, T> {
157158
}
158159
}
159160

161+
#[allow(clippy::needless_lifetimes)]
160162
impl<'a, T: Row<'a>> Row<'a> for Vec<T> {
161163
type ReadResult = ArrayGetter<'a, T>;
162164

@@ -218,6 +220,7 @@ impl<'a, T1: Row<'a> + Ord, T2: Row<'a> + Ord> MapGetter<'a, T1, T2> {
218220
}
219221
}
220222

223+
#[allow(clippy::needless_lifetimes)]
221224
impl<'a, T1: Row<'a> + Ord, T2: Row<'a> + Ord> Row<'a> for BTreeMap<T1, T2> {
222225
type ReadResult = MapGetter<'a, T1, T2>;
223226

rust/fory-core/src/util.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,25 @@ impl<T> Spinlock<T> {
174174
}
175175
}
176176

177+
#[allow(clippy::needless_lifetimes)]
177178
pub struct SpinlockGuard<'a, T> {
178179
lock: &'a Spinlock<T>,
179180
}
180-
181+
#[allow(clippy::needless_lifetimes)]
181182
impl<'a, T> Drop for SpinlockGuard<'a, T> {
182183
fn drop(&mut self) {
183184
self.lock.unlock();
184185
}
185186
}
186-
187+
#[allow(clippy::needless_lifetimes)]
187188
impl<'a, T> Deref for SpinlockGuard<'a, T> {
188189
type Target = T;
189190
fn deref(&self) -> &Self::Target {
190191
unsafe { &*self.lock.data.get() }
191192
}
192193
}
193194

195+
#[allow(clippy::needless_lifetimes)]
194196
impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
195197
fn deref_mut(&mut self) -> &mut Self::Target {
196198
unsafe { &mut *self.lock.data.get() }

0 commit comments

Comments
 (0)