f2rust_std 0.1.0

Standard library for FORTRAN-to-Rust translated programs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use crate::util::{offset_1d, offset_2d, offset_3d, offset_4d, parse_bounds};
use std::ops::{Index, IndexMut, RangeBounds, RangeInclusive};
use std::slice::GetDisjointMutError;

/// FORTRAN-style CHARACTER arrays.
///
/// These have peculiar behaviour in FORTRAN: they are stored in memory as
/// a contiguous array of bytes, split into an array of equal-length strings, but that length
/// can vary.
///
/// If a function declares a dummy argument as `CHARACTER*(*)`, it will use the same string
/// length as the actual argument provided by the caller. But if it declares `CHARACTER*(N)`,
/// the same bytes will be reinterpreted as strings of length `N`.
///
/// To support this, the translated APIs use `CharArray` which wraps a `&[u8]` slice and a string
/// length. Functions using `DummyCharArray` can either adopt this length or replace it.
pub struct CharArray<'a> {
    data: &'a [u8],
    element_length: usize,
}

impl<'a> CharArray<'a> {
    /// Constructs an array over a sequence of characters, split into strings
    /// of size `element_length`
    pub fn new(data: &'a [u8], element_length: usize) -> Self {
        assert_eq!(data.len() % element_length, 0);
        Self {
            data,
            element_length,
        }
    }

    /// Constructs a single-element array.
    pub fn from_ref(data: &'a [u8]) -> Self {
        let element_length = data.len();
        Self {
            data,
            element_length,
        }
    }

    pub fn to_owned(&self) -> OwnedCharArray {
        OwnedCharArray {
            data: self.data.to_vec(),
            element_length: self.element_length,
        }
    }

    pub fn element_length(&self) -> usize {
        self.element_length
    }
}

/// FORTRAN-style CHARACTER arrays.
///
/// Mutable version of [`CharArray`].
pub struct CharArrayMut<'a> {
    data: &'a mut [u8],
    element_length: usize,
}

impl<'a> CharArrayMut<'a> {
    /// Constructs an array over a sequence of characters, split into strings
    /// of size `element_length`
    pub fn new(data: &'a mut [u8], element_length: usize) -> Self {
        assert_eq!(data.len() % element_length, 0);
        Self {
            data,
            element_length,
        }
    }

    /// Constructs a single-element array.
    pub fn from_mut(data: &'a mut [u8]) -> Self {
        let element_length = data.len();
        Self {
            data,
            element_length,
        }
    }

    pub fn to_owned(&self) -> OwnedCharArray {
        OwnedCharArray {
            data: self.data.to_vec(),
            element_length: self.element_length,
        }
    }

    pub fn element_length(&self) -> usize {
        self.element_length
    }
}

pub struct OwnedCharArray {
    data: Vec<u8>,
    element_length: usize,
}

impl OwnedCharArray {
    pub fn as_arg(&self) -> CharArray {
        CharArray {
            data: &self.data,
            element_length: self.element_length,
        }
    }

    pub fn as_arg_mut(&mut self) -> CharArrayMut {
        CharArrayMut {
            data: &mut self.data,
            element_length: self.element_length,
        }
    }

    pub fn element_length(&self) -> usize {
        self.element_length
    }
}

macro_rules! define_array {
    ($dims:expr, $actual:ident, $dummy:ident, $dummy_mut:ident, $offset:ident, $index:ty,
        ($($bn:ident: $Bn:ident),+)) => {
        /// Implementation of CHARACTER arrays used as actual arguments,
        /// which own their data.
        pub struct $actual {
            data: Vec<u8>,
            bounds: [(i32, i32); $dims],
            element_length: usize,
        }

        /// Implementation of CHARACTER arrays used as dummy arguments,
        /// which don't own their data.
        pub struct $dummy<'a> {
            data: &'a [u8],
            bounds: [(i32, i32); $dims],
            element_length: usize,
        }

        /// Implementation of CHARACTER arrays used as dummy arguments,
        /// which don't own their data.
        pub struct $dummy_mut<'a> {
            data: &'a mut [u8],
            bounds: [(i32, i32); $dims],
            element_length: usize,
        }

        impl $actual {
            // Use RangeInclusive instead of RangeBounds, to enforce an upper limit
            pub fn new(element_length: i32, $($bn: RangeInclusive<i32>),+) -> Self {
                let bounds = [$(parse_bounds($bn)),+];
                let size = bounds
                    .iter()
                    .map(|(lower, upper)| (upper - lower + 1).max(0))
                    .product::<i32>();

                let len = element_length as usize;

                Self {
                    data: vec![b' '; len * size as usize],
                    bounds,
                    element_length: len,
                }
            }
        }

        impl<'a> $dummy<'a> {
            pub fn new<$($Bn: RangeBounds<i32>),+>(
                r: CharArray<'a>,
                element_length: Option<i32>,
                $($bn: $Bn),+
            ) -> Self {
                let bounds = [$(parse_bounds($bn)),+];

                let len = element_length
                    .map(|n| n as usize)
                    .unwrap_or(r.element_length);

                if bounds.last().unwrap().1 == i32::MAX {
                    Self {
                        data: r.data,
                        bounds,
                        element_length: len,
                    }
                } else {
                    let size = bounds
                        .iter()
                        .map(|(lower, upper)| (upper - lower + 1).max(0))
                        .product::<i32>();
                    Self {
                        data: &r.data[0..len * size as usize],
                        bounds,
                        element_length: len,
                    }
                }
            }
        }

        impl<'a> $dummy_mut<'a> {
            pub fn new<$($Bn: RangeBounds<i32>),+>(
                r: CharArrayMut<'a>,
                element_length: Option<i32>,
                $($bn: $Bn),+
            ) -> Self {
                let bounds = [$(parse_bounds($bn)),+];

                let len = element_length
                    .map(|n| n as usize)
                    .unwrap_or(r.element_length);

                if bounds.last().unwrap().1 == i32::MAX {
                    Self {
                        data: r.data,
                        bounds,
                        element_length: len,
                    }
                } else {
                    let size = bounds
                        .iter()
                        .map(|(lower, upper)| (upper - lower + 1).max(0))
                        .product::<i32>();
                    Self {
                        data: &mut r.data[0..len * size as usize],
                        bounds,
                        element_length: len,
                    }
                }
            }
        }

        impl Index<$index> for $actual {
            type Output = [u8];

            fn index(&self, index: $index) -> &Self::Output {
                let offset = self.offset(index);
                &self.data[offset..offset + self.element_length]
            }
        }

        impl Index<$index> for $dummy<'_> {
            type Output = [u8];

            fn index(&self, index: $index) -> &Self::Output {
                let offset = self.offset(index);
                &self.data[offset..offset + self.element_length]
            }
        }

        impl Index<$index> for $dummy_mut<'_> {
            type Output = [u8];

            fn index(&self, index: $index) -> &Self::Output {
                let offset = self.offset(index);
                &self.data[offset..offset + self.element_length]
            }
        }

        impl IndexMut<$index> for $actual {
            fn index_mut(&mut self, index: $index) -> &mut Self::Output {
                let offset = self.offset(index);
                &mut self.data[offset..offset + self.element_length]
            }
        }

        impl IndexMut<$index> for $dummy_mut<'_> {
            fn index_mut(&mut self, index: $index) -> &mut Self::Output {
                let offset = self.offset(index);
                &mut self.data[offset..offset + self.element_length]
            }
        }

        impl CharArrayOps<$index> for $actual {
            fn data(&self) -> &[u8] {
                &self.data
            }

            fn element_length(&self) -> usize {
                self.element_length
            }

            fn byte_offset(&self, index: $index) -> usize {
                $offset(self.bounds, index)
            }
        }

        impl CharArrayOpsMut<$index> for $actual {
            fn data_mut(&mut self) -> &mut [u8] {
                &mut self.data
            }
        }

        impl CharArrayOps<$index> for $dummy<'_> {
            fn data(& self) -> &[u8] {
                self.data
            }

            fn element_length(&self) -> usize {
                self.element_length
            }

            fn byte_offset(&self, index: $index) -> usize {
                $offset(self.bounds, index)
            }
        }

        impl CharArrayOps<$index> for $dummy_mut<'_> {
            fn data(& self) -> &[u8] {
                self.data
            }

            fn element_length(&self) -> usize {
                self.element_length
            }

            fn byte_offset(&self, index: $index) -> usize {
                $offset(self.bounds, index)
            }
        }

        impl CharArrayOpsMut<$index> for $dummy_mut<'_> {
            fn data_mut(&mut self) -> &mut [u8] {
                self.data
            }
        }
    }
}

define_array!(1, ActualCharArray, DummyCharArray, DummyCharArrayMut, offset_1d, i32, (b0: B0));
define_array!(2, ActualCharArray2D, DummyCharArray2D, DummyCharArrayMut2D, offset_2d, [i32; 2], (b0: B0, b1: B1));
define_array!(3, ActualCharArray3D, DummyCharArray3D, DummyCharArrayMut3D, offset_3d, [i32; 3], (b0: B0, b1: B1, b2: B2));
define_array!(4, ActualCharArray4D, DummyCharArray4D, DummyCharArrayMut4D, offset_4d, [i32; 4], (b0: B0, b1: B1, b2: B2, b3: B3));

pub trait CharArrayOps<I> {
    fn data(&self) -> &[u8];
    fn element_length(&self) -> usize;
    fn byte_offset(&self, index: I) -> usize;

    fn offset(&self, index: I) -> usize {
        self.byte_offset(index) * self.element_length()
    }

    fn to_owned(&self) -> OwnedCharArray {
        let element_length = self.element_length();
        OwnedCharArray {
            data: self.data().to_vec(),
            element_length,
        }
    }

    fn first(&self) -> &[u8] {
        let element_length = self.element_length();
        &self.data()[0..element_length]
    }

    fn as_arg(&self) -> CharArray {
        let element_length = self.element_length();
        CharArray {
            data: self.data(),
            element_length,
        }
    }

    // We can't use Index for element access, because `a[i]` is `*a.index(i)`
    // so it has type `[u8]`, and we want &[u8] for consistency with our other
    // character types. (TODO: or we could just improve the compiler?)
    fn get(&self, index: I) -> &[u8] {
        let offset = self.offset(index);
        let element_length = self.element_length();
        &self.data()[offset..offset + element_length]
    }

    fn subarray(&self, index: I) -> CharArray {
        let offset = self.offset(index);
        let element_length = self.element_length();
        CharArray {
            data: &self.data()[offset..],
            element_length,
        }
    }

    fn subscript(&self, index: I) -> i32 {
        self.byte_offset(index) as i32 + 1
    }
}

pub trait CharArrayOpsMut<I>: CharArrayOps<I> {
    fn data_mut(&mut self) -> &mut [u8];

    fn first_mut(&mut self) -> &mut [u8] {
        let element_length = self.element_length();
        &mut self.data_mut()[0..element_length]
    }

    fn as_arg_mut(&mut self) -> CharArrayMut {
        let element_length = self.element_length();
        CharArrayMut {
            data: self.data_mut(),
            element_length,
        }
    }

    fn get_mut(&mut self, index: I) -> &mut [u8] {
        let offset = self.offset(index);
        let element_length = self.element_length();
        &mut self.data_mut()[offset..offset + element_length]
    }

    fn iter_mut(&mut self) -> impl Iterator<Item = &mut [u8]> {
        let element_length = self.element_length();
        self.data_mut().chunks_mut(element_length)
    }

    fn subarray_mut(&mut self, index: I) -> CharArrayMut {
        let offset = self.offset(index);
        let element_length = self.element_length();
        CharArrayMut {
            data: &mut self.data_mut()[offset..],
            element_length,
        }
    }

    fn get_disjoint_mut<const N: usize>(
        &mut self,
        indices: [I; N],
    ) -> Result<[&mut [u8]; N], GetDisjointMutError> {
        let offsets = indices.map(|index| self.offset(index));
        let ranges = offsets.map(|n| n..n + self.element_length());
        self.data_mut().get_disjoint_mut(ranges)
    }
}