Skip to content

Commit c8d0fef

Browse files
committed
Remove unnecessary files and code leftover from indexing feature
1 parent 40d1b2f commit c8d0fef

File tree

5 files changed

+65
-94
lines changed

5 files changed

+65
-94
lines changed
-1.35 KB
Binary file not shown.

data/iceland-surface.grib.idx

-399 Bytes
Binary file not shown.

src/codes_handle/iterator.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,68 @@
11
use fallible_iterator::FallibleIterator;
22

3-
use crate::{
4-
ArcMessage, CodesFile, RefMessage,
5-
codes_handle::{HandleGenerator, ThreadSafeHandle},
6-
errors::CodesError,
7-
};
3+
use crate::{ArcMessage, CodesFile, RefMessage, errors::CodesError};
4+
use std::fmt::Debug;
85
use std::sync::{Arc, Mutex};
96

107
#[derive(Debug)]
11-
pub struct RefMessageIter<'a, S: HandleGenerator> {
12-
codes_handle: &'a mut CodesFile<S>,
8+
pub struct RefMessageIter<'a, D: Debug> {
9+
codes_file: &'a mut CodesFile<D>,
1310
}
1411

15-
impl<S: HandleGenerator> CodesFile<S> {
16-
pub fn ref_message_iter<'a>(&'a mut self) -> RefMessageIter<'a, S> {
17-
RefMessageIter { codes_handle: self }
12+
impl<D: Debug> CodesFile<D> {
13+
pub fn ref_message_iter<'a>(&'a mut self) -> RefMessageIter<'a, D> {
14+
RefMessageIter { codes_file: self }
1815
}
1916
}
2017

2118
/// # Errors
2219
///
2320
/// The `next()` will return [`CodesInternal`](crate::errors::CodesInternal)
2421
/// when internal ecCodes function returns non-zero code.
25-
impl<'ch, S: HandleGenerator> FallibleIterator for RefMessageIter<'ch, S> {
22+
impl<'ch, D: Debug> FallibleIterator for RefMessageIter<'ch, D> {
2623
type Item = RefMessage<'ch>;
2724
type Error = CodesError;
2825

2926
fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> {
30-
let new_eccodes_handle = self.codes_handle.source.gen_codes_handle()?;
27+
let eccodes_handle = self.codes_file.generate_codes_handle()?;
3128

32-
if new_eccodes_handle.is_null() {
29+
if eccodes_handle.is_null() {
3330
Ok(None)
3431
} else {
35-
Ok(Some(RefMessage::new(new_eccodes_handle)))
32+
Ok(Some(RefMessage::new(eccodes_handle)))
3633
}
3734
}
3835
}
3936

4037
#[derive(Debug)]
41-
pub struct ArcMessageIter<S: ThreadSafeHandle> {
42-
codes_handle: Arc<Mutex<CodesFile<S>>>,
38+
pub struct ArcMessageIter<D: Debug> {
39+
codes_file: Arc<Mutex<CodesFile<D>>>,
4340
}
44-
impl<S: ThreadSafeHandle> CodesFile<S> {
45-
pub fn arc_message_iter(self) -> ArcMessageIter<S> {
41+
impl<D: Debug> CodesFile<D> {
42+
pub fn arc_message_iter(self) -> ArcMessageIter<D> {
4643
ArcMessageIter {
47-
codes_handle: Arc::new(Mutex::new(self)),
44+
codes_file: Arc::new(Mutex::new(self)),
4845
}
4946
}
5047
}
5148

52-
impl<S: ThreadSafeHandle> FallibleIterator for ArcMessageIter<S> {
53-
type Item = ArcMessage<S>;
49+
impl<D: Debug> FallibleIterator for ArcMessageIter<D> {
50+
type Item = ArcMessage<D>;
5451

5552
type Error = CodesError;
5653

5754
fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> {
58-
let new_eccodes_handle = self
59-
.codes_handle
55+
let eccodes_handle = self
56+
.codes_file
6057
.lock()
6158
// This mutex can be poisoned only when thread that holds ArcMessageIter panics, which would make using the mutex impossible")
6259
.unwrap()
63-
.source
64-
.gen_codes_handle()?;
60+
.generate_codes_handle()?;
6561

66-
if new_eccodes_handle.is_null() {
62+
if eccodes_handle.is_null() {
6763
Ok(None)
6864
} else {
69-
Ok(Some(ArcMessage::new(
70-
new_eccodes_handle,
71-
&self.codes_handle,
72-
)))
65+
Ok(Some(ArcMessage::new(eccodes_handle, &self.codes_file)))
7366
}
7467
}
7568
}

src/codes_handle/mod.rs

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,6 @@ pub use iterator::{ArcMessageIter, RefMessageIter};
1717

1818
mod iterator;
1919

20-
/// This is an internal structure used to access provided file by `CodesHandle`.
21-
/// It also allows to differentiate between `CodesFile` created from file and from index.
22-
/// It is not intended to be used directly by the user.
23-
#[doc(hidden)]
24-
#[derive(Debug)]
25-
pub struct CodesFileSource<D: Debug> {
26-
// fields dropped from top
27-
pointer: *mut FILE,
28-
product_kind: ProductKind,
29-
_data: D,
30-
}
31-
32-
/// Marker trait to differentiate between `CodesHandle` created from index and file/buffer.
33-
#[doc(hidden)]
34-
pub trait ThreadSafeHandle: HandleGenerator {}
35-
36-
impl ThreadSafeHandle for CodesFileSource<Vec<u8>> {}
37-
impl ThreadSafeHandle for CodesFileSource<File> {}
38-
39-
/// Internal trait implemented for types that can be called to generate `*mut codes_handle`.
40-
#[doc(hidden)]
41-
pub trait HandleGenerator: Debug {
42-
fn gen_codes_handle(&mut self) -> Result<*mut codes_handle, CodesError>;
43-
}
44-
45-
impl<D: Debug> HandleGenerator for CodesFileSource<D> {
46-
fn gen_codes_handle(&mut self) -> Result<*mut codes_handle, CodesError> {
47-
unsafe { codes_handle_new_from_file(self.pointer, self.product_kind) }
48-
}
49-
}
50-
5120
/// Structure providing access to the GRIB file which takes a full ownership of the accessed file.
5221
///
5322
/// It can be constructed from:
@@ -117,8 +86,11 @@ impl<D: Debug> HandleGenerator for CodesFileSource<D> {
11786
///
11887
/// All available methods for `CodesHandle` iterator can be found in [`FallibleStreamingIterator`](crate::FallibleStreamingIterator) trait.
11988
#[derive(Debug)]
120-
pub struct CodesFile<S: HandleGenerator> {
121-
source: S,
89+
pub struct CodesFile<D: Debug> {
90+
// fields are dropped from top
91+
pointer: *mut FILE,
92+
product_kind: ProductKind,
93+
_data: D,
12294
}
12395

12496
// 2024-07-26
@@ -135,6 +107,12 @@ pub struct CodesFile<S: HandleGenerator> {
135107
// Clearing the memory is handled on ecCodes side by KeyedMessage/CodesIndex destructors
136108
// and on rust side by destructors of data_container we own.
137109

110+
impl<D: Debug> CodesFile<D> {
111+
fn generate_codes_handle(&mut self) -> Result<*mut codes_handle, CodesError> {
112+
unsafe { codes_handle_new_from_file(self.pointer, self.product_kind) }
113+
}
114+
}
115+
138116
/// Enum representing the kind of product (file type) inside handled file.
139117
/// Used to indicate to ecCodes how it should decode/encode messages.
140118
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
@@ -143,7 +121,7 @@ pub enum ProductKind {
143121
GRIB = ProductKind_PRODUCT_GRIB as isize,
144122
}
145123

146-
impl CodesFile<CodesFileSource<File>> {
124+
impl CodesFile<File> {
147125
///Opens file at given [`Path`] as selected [`ProductKind`] and contructs `CodesHandle`.
148126
///
149127
///## Example
@@ -188,15 +166,13 @@ impl CodesFile<CodesFileSource<File>> {
188166
let file_pointer = open_with_fdopen(&file)?;
189167

190168
Ok(Self {
191-
source: CodesFileSource {
192-
_data: file,
193-
pointer: file_pointer,
194-
product_kind,
195-
},
169+
_data: file,
170+
pointer: file_pointer,
171+
product_kind,
196172
})
197173
}
198174
}
199-
impl CodesFile<CodesFileSource<Vec<u8>>> {
175+
impl CodesFile<Vec<u8>> {
200176
///Opens data in provided buffer as selected [`ProductKind`] and contructs `CodesHandle`.
201177
///
202178
///## Example
@@ -239,11 +215,9 @@ impl CodesFile<CodesFileSource<Vec<u8>>> {
239215
let file_pointer = open_with_fmemopen(&file_data)?;
240216

241217
Ok(Self {
242-
source: CodesFileSource {
243-
_data: file_data,
244-
product_kind,
245-
pointer: file_pointer,
246-
},
218+
_data: file_data,
219+
product_kind,
220+
pointer: file_pointer,
247221
})
248222
}
249223
}
@@ -297,14 +271,14 @@ mod tests {
297271
let file_path = Path::new("./data/iceland.grib");
298272
let product_kind = ProductKind::GRIB;
299273

300-
let handle = CodesFile::new_from_file(file_path, product_kind)?;
274+
let codes_file = CodesFile::new_from_file(file_path, product_kind)?;
301275

302-
assert!(!handle.source.pointer.is_null());
303-
assert_eq!(handle.source.product_kind as u32, {
276+
assert!(!codes_file.pointer.is_null());
277+
assert_eq!(codes_file.product_kind as u32, {
304278
ProductKind_PRODUCT_GRIB
305279
});
306280

307-
handle.source._data.metadata()?;
281+
codes_file._data.metadata()?;
308282

309283
Ok(())
310284
}
@@ -317,13 +291,13 @@ mod tests {
317291
let mut buf = Vec::new();
318292
f.read_to_end(&mut buf)?;
319293

320-
let handle = CodesFile::new_from_memory(buf, product_kind)?;
321-
assert!(!handle.source.pointer.is_null());
322-
assert_eq!(handle.source.product_kind as u32, {
294+
let codes_file = CodesFile::new_from_memory(buf, product_kind)?;
295+
assert!(!codes_file.pointer.is_null());
296+
assert_eq!(codes_file.product_kind as u32, {
323297
ProductKind_PRODUCT_GRIB
324298
});
325299

326-
assert!(!handle.source._data.is_empty());
300+
assert!(!codes_file._data.is_empty());
327301

328302
Ok(())
329303
}

src/codes_message/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ pub use read::{DynamicKeyType, KeyPropertiesRead, KeyRead};
1111
pub use write::KeyWrite;
1212

1313
use eccodes_sys::codes_handle;
14-
use std::{fmt::Debug, hash::Hash, marker::PhantomData, ptr::null_mut, sync::{Arc, Mutex}};
14+
use std::{
15+
fmt::Debug,
16+
hash::Hash,
17+
marker::PhantomData,
18+
ptr::null_mut,
19+
sync::{Arc, Mutex},
20+
};
1521
use tracing::{Level, event, instrument};
1622

17-
use crate::{
18-
CodesFile, codes_handle::ThreadSafeHandle, intermediate_bindings::codes_handle_delete,
19-
};
23+
use crate::{CodesFile, intermediate_bindings::codes_handle_delete};
2024

2125
/// Structure that provides access to the data contained in the GRIB file, which directly corresponds to the message in the GRIB file
2226
///
@@ -54,10 +58,10 @@ pub type RefMessage<'ch> = CodesMessage<RefParent<'ch>>;
5458
/// requiring `&mut self`. As `AtomicMessage` implements `Send + Sync` this exclusive method access is not
5559
/// guaranteed with just `&self`. `AtomicMessage` also implements a minimal subset of functionalities
5660
/// to limit the risk of some internal ecCodes functions not being thread-safe.
57-
pub type ArcMessage<S> = CodesMessage<ArcParent<S>>;
61+
pub type ArcMessage<D> = CodesMessage<ArcParent<D>>;
5862

59-
unsafe impl<S: ThreadSafeHandle> Send for ArcMessage<S> {}
60-
unsafe impl<S: ThreadSafeHandle> Sync for ArcMessage<S> {}
63+
unsafe impl<D: Debug> Send for ArcMessage<D> {}
64+
unsafe impl<D: Debug> Sync for ArcMessage<D> {}
6165

6266
pub type BufMessage = CodesMessage<BufParent>;
6367

@@ -85,8 +89,8 @@ pub struct BufParent();
8589

8690
#[derive(Debug)]
8791
#[doc(hidden)]
88-
pub struct ArcParent<S: ThreadSafeHandle> {
89-
_arc_handle: Arc<Mutex<CodesFile<S>>>,
92+
pub struct ArcParent<D: Debug> {
93+
_arc_handle: Arc<Mutex<CodesFile<D>>>,
9094
}
9195

9296
impl RefMessage<'_> {
@@ -98,8 +102,8 @@ impl RefMessage<'_> {
98102
}
99103
}
100104

101-
impl<S: ThreadSafeHandle> ArcMessage<S> {
102-
pub(crate) fn new(handle: *mut codes_handle, parent: &Arc<Mutex<CodesFile<S>>>) -> Self {
105+
impl<D: Debug> ArcMessage<D> {
106+
pub(crate) fn new(handle: *mut codes_handle, parent: &Arc<Mutex<CodesFile<D>>>) -> Self {
103107
ArcMessage {
104108
_parent: ArcParent {
105109
_arc_handle: parent.clone(),

0 commit comments

Comments
 (0)