redrust 0.1.1

redrust is a port of the popular Redis database system written in Rust programming language. This port aims to provide all the features of Redis while taking advantage of the Rust language's safety, speed, and modern language features.
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
extern crate c2rust_bitfields;
extern crate libc;
extern crate core;
pub type __int8_t = libc::c_schar;
pub type __uint8_t = libc::c_uchar;
pub type __uint32_t = libc::c_uint;
pub type __uint64_t = libc::c_ulong;
pub type int8_t = __int8_t;
pub type uint8_t = __uint8_t;
pub type uint32_t = __uint32_t;
pub type uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GeoHashBits {
    pub bits: uint64_t,
    pub step: uint8_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GeoHashRange {
    pub min: libc::c_double,
    pub max: libc::c_double,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GeoHashArea {
    pub hash: GeoHashBits,
    pub longitude: GeoHashRange,
    pub latitude: GeoHashRange,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GeoHashNeighbors {
    pub north: GeoHashBits,
    pub east: GeoHashBits,
    pub west: GeoHashBits,
    pub south: GeoHashBits,
    pub north_east: GeoHashBits,
    pub south_east: GeoHashBits,
    pub north_west: GeoHashBits,
    pub south_west: GeoHashBits,
}
#[inline]
unsafe extern "C" fn interleave64(mut xlo: uint32_t, mut ylo: uint32_t) -> uint64_t {
    static mut B: [uint64_t; 5] = [
        0x5555555555555555 as libc::c_ulonglong as uint64_t,
        0x3333333333333333 as libc::c_ulonglong as uint64_t,
        0xf0f0f0f0f0f0f0f as libc::c_ulonglong as uint64_t,
        0xff00ff00ff00ff as libc::c_ulonglong as uint64_t,
        0xffff0000ffff as libc::c_ulonglong as uint64_t,
    ];
    static mut S: [libc::c_uint; 5] = [
        1 as libc::c_int as libc::c_uint,
        2 as libc::c_int as libc::c_uint,
        4 as libc::c_int as libc::c_uint,
        8 as libc::c_int as libc::c_uint,
        16 as libc::c_int as libc::c_uint,
    ];
    let mut x: uint64_t = xlo as uint64_t;
    let mut y: uint64_t = ylo as uint64_t;
    x = (x | x << S[4 as libc::c_int as usize]) & B[4 as libc::c_int as usize];
    y = (y | y << S[4 as libc::c_int as usize]) & B[4 as libc::c_int as usize];
    x = (x | x << S[3 as libc::c_int as usize]) & B[3 as libc::c_int as usize];
    y = (y | y << S[3 as libc::c_int as usize]) & B[3 as libc::c_int as usize];
    x = (x | x << S[2 as libc::c_int as usize]) & B[2 as libc::c_int as usize];
    y = (y | y << S[2 as libc::c_int as usize]) & B[2 as libc::c_int as usize];
    x = (x | x << S[1 as libc::c_int as usize]) & B[1 as libc::c_int as usize];
    y = (y | y << S[1 as libc::c_int as usize]) & B[1 as libc::c_int as usize];
    x = (x | x << S[0 as libc::c_int as usize]) & B[0 as libc::c_int as usize];
    y = (y | y << S[0 as libc::c_int as usize]) & B[0 as libc::c_int as usize];
    return x | y << 1 as libc::c_int;
}
#[inline]
unsafe extern "C" fn deinterleave64(mut interleaved: uint64_t) -> uint64_t {
    static mut B: [uint64_t; 6] = [
        0x5555555555555555 as libc::c_ulonglong as uint64_t,
        0x3333333333333333 as libc::c_ulonglong as uint64_t,
        0xf0f0f0f0f0f0f0f as libc::c_ulonglong as uint64_t,
        0xff00ff00ff00ff as libc::c_ulonglong as uint64_t,
        0xffff0000ffff as libc::c_ulonglong as uint64_t,
        0xffffffff as libc::c_ulonglong as uint64_t,
    ];
    static mut S: [libc::c_uint; 6] = [
        0 as libc::c_int as libc::c_uint,
        1 as libc::c_int as libc::c_uint,
        2 as libc::c_int as libc::c_uint,
        4 as libc::c_int as libc::c_uint,
        8 as libc::c_int as libc::c_uint,
        16 as libc::c_int as libc::c_uint,
    ];
    let mut x: uint64_t = interleaved;
    let mut y: uint64_t = interleaved >> 1 as libc::c_int;
    x = (x | x >> S[0 as libc::c_int as usize]) & B[0 as libc::c_int as usize];
    y = (y | y >> S[0 as libc::c_int as usize]) & B[0 as libc::c_int as usize];
    x = (x | x >> S[1 as libc::c_int as usize]) & B[1 as libc::c_int as usize];
    y = (y | y >> S[1 as libc::c_int as usize]) & B[1 as libc::c_int as usize];
    x = (x | x >> S[2 as libc::c_int as usize]) & B[2 as libc::c_int as usize];
    y = (y | y >> S[2 as libc::c_int as usize]) & B[2 as libc::c_int as usize];
    x = (x | x >> S[3 as libc::c_int as usize]) & B[3 as libc::c_int as usize];
    y = (y | y >> S[3 as libc::c_int as usize]) & B[3 as libc::c_int as usize];
    x = (x | x >> S[4 as libc::c_int as usize]) & B[4 as libc::c_int as usize];
    y = (y | y >> S[4 as libc::c_int as usize]) & B[4 as libc::c_int as usize];
    x = (x | x >> S[5 as libc::c_int as usize]) & B[5 as libc::c_int as usize];
    y = (y | y >> S[5 as libc::c_int as usize]) & B[5 as libc::c_int as usize];
    return x | y << 32 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn geohashGetCoordRange(
    mut long_range: *mut GeoHashRange,
    mut lat_range: *mut GeoHashRange,
) {
    (*long_range).max = 180 as libc::c_int as libc::c_double;
    (*long_range).min = -(180 as libc::c_int) as libc::c_double;
    (*lat_range).max = 85.05112878f64;
    (*lat_range).min = -85.05112878f64;
}
#[no_mangle]
pub unsafe extern "C" fn geohashEncode(
    mut long_range: *const GeoHashRange,
    mut lat_range: *const GeoHashRange,
    mut longitude: libc::c_double,
    mut latitude: libc::c_double,
    mut step: uint8_t,
    mut hash: *mut GeoHashBits,
) -> libc::c_int {
    if hash.is_null() || step as libc::c_int > 32 as libc::c_int
        || step as libc::c_int == 0 as libc::c_int
        || (lat_range.is_null() || (*lat_range).max == 0. && (*lat_range).min == 0.)
        || (long_range.is_null() || (*long_range).max == 0. && (*long_range).min == 0.)
    {
        return 0 as libc::c_int;
    }
    if longitude > 180 as libc::c_int as libc::c_double
        || longitude < -(180 as libc::c_int) as libc::c_double
        || latitude > 85.05112878f64 || latitude < -85.05112878f64
    {
        return 0 as libc::c_int;
    }
    (*hash).bits = 0 as libc::c_int as uint64_t;
    (*hash).step = step;
    if latitude < (*lat_range).min || latitude > (*lat_range).max
        || longitude < (*long_range).min || longitude > (*long_range).max
    {
        return 0 as libc::c_int;
    }
    let mut lat_offset: libc::c_double = (latitude - (*lat_range).min)
        / ((*lat_range).max - (*lat_range).min);
    let mut long_offset: libc::c_double = (longitude - (*long_range).min)
        / ((*long_range).max - (*long_range).min);
    lat_offset *= ((1 as libc::c_ulonglong) << step as libc::c_int) as libc::c_double;
    long_offset *= ((1 as libc::c_ulonglong) << step as libc::c_int) as libc::c_double;
    (*hash).bits = interleave64(lat_offset as uint32_t, long_offset as uint32_t);
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn geohashEncodeType(
    mut longitude: libc::c_double,
    mut latitude: libc::c_double,
    mut step: uint8_t,
    mut hash: *mut GeoHashBits,
) -> libc::c_int {
    let mut r: [GeoHashRange; 2] = [
        {
            let mut init = GeoHashRange {
                min: 0 as libc::c_int as libc::c_double,
                max: 0.,
            };
            init
        },
        GeoHashRange { min: 0., max: 0. },
    ];
    geohashGetCoordRange(
        &mut *r.as_mut_ptr().offset(0 as libc::c_int as isize),
        &mut *r.as_mut_ptr().offset(1 as libc::c_int as isize),
    );
    return geohashEncode(
        &mut *r.as_mut_ptr().offset(0 as libc::c_int as isize),
        &mut *r.as_mut_ptr().offset(1 as libc::c_int as isize),
        longitude,
        latitude,
        step,
        hash,
    );
}
#[no_mangle]
pub unsafe extern "C" fn geohashEncodeWGS84(
    mut longitude: libc::c_double,
    mut latitude: libc::c_double,
    mut step: uint8_t,
    mut hash: *mut GeoHashBits,
) -> libc::c_int {
    return geohashEncodeType(longitude, latitude, step, hash);
}
#[no_mangle]
pub unsafe extern "C" fn geohashDecode(
    long_range: GeoHashRange,
    lat_range: GeoHashRange,
    hash: GeoHashBits,
    mut area: *mut GeoHashArea,
) -> libc::c_int {
    if hash.bits == 0 && hash.step == 0 || area.is_null()
        || lat_range.max == 0. && lat_range.min == 0.
        || long_range.max == 0. && long_range.min == 0.
    {
        return 0 as libc::c_int;
    }
    (*area).hash = hash;
    let mut step: uint8_t = hash.step;
    let mut hash_sep: uint64_t = deinterleave64(hash.bits);
    let mut lat_scale: libc::c_double = lat_range.max - lat_range.min;
    let mut long_scale: libc::c_double = long_range.max - long_range.min;
    let mut ilato: uint32_t = hash_sep as uint32_t;
    let mut ilono: uint32_t = (hash_sep >> 32 as libc::c_int) as uint32_t;
    (*area)
        .latitude
        .min = lat_range.min
        + ilato as libc::c_double * 1.0f64
            / ((1 as libc::c_ulonglong) << step as libc::c_int) as libc::c_double
            * lat_scale;
    (*area)
        .latitude
        .max = lat_range.min
        + ilato.wrapping_add(1 as libc::c_int as libc::c_uint) as libc::c_double * 1.0f64
            / ((1 as libc::c_ulonglong) << step as libc::c_int) as libc::c_double
            * lat_scale;
    (*area)
        .longitude
        .min = long_range.min
        + ilono as libc::c_double * 1.0f64
            / ((1 as libc::c_ulonglong) << step as libc::c_int) as libc::c_double
            * long_scale;
    (*area)
        .longitude
        .max = long_range.min
        + ilono.wrapping_add(1 as libc::c_int as libc::c_uint) as libc::c_double * 1.0f64
            / ((1 as libc::c_ulonglong) << step as libc::c_int) as libc::c_double
            * long_scale;
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn geohashDecodeType(
    hash: GeoHashBits,
    mut area: *mut GeoHashArea,
) -> libc::c_int {
    let mut r: [GeoHashRange; 2] = [
        {
            let mut init = GeoHashRange {
                min: 0 as libc::c_int as libc::c_double,
                max: 0.,
            };
            init
        },
        GeoHashRange { min: 0., max: 0. },
    ];
    geohashGetCoordRange(
        &mut *r.as_mut_ptr().offset(0 as libc::c_int as isize),
        &mut *r.as_mut_ptr().offset(1 as libc::c_int as isize),
    );
    return geohashDecode(
        r[0 as libc::c_int as usize],
        r[1 as libc::c_int as usize],
        hash,
        area,
    );
}
#[no_mangle]
pub unsafe extern "C" fn geohashDecodeWGS84(
    hash: GeoHashBits,
    mut area: *mut GeoHashArea,
) -> libc::c_int {
    return geohashDecodeType(hash, area);
}
#[no_mangle]
pub unsafe extern "C" fn geohashDecodeAreaToLongLat(
    mut area: *const GeoHashArea,
    mut xy: *mut libc::c_double,
) -> libc::c_int {
    if xy.is_null() {
        return 0 as libc::c_int;
    }
    *xy
        .offset(
            0 as libc::c_int as isize,
        ) = ((*area).longitude.min + (*area).longitude.max)
        / 2 as libc::c_int as libc::c_double;
    if *xy.offset(0 as libc::c_int as isize) > 180 as libc::c_int as libc::c_double {
        *xy.offset(0 as libc::c_int as isize) = 180 as libc::c_int as libc::c_double;
    }
    if *xy.offset(0 as libc::c_int as isize) < -(180 as libc::c_int) as libc::c_double {
        *xy.offset(0 as libc::c_int as isize) = -(180 as libc::c_int) as libc::c_double;
    }
    *xy
        .offset(
            1 as libc::c_int as isize,
        ) = ((*area).latitude.min + (*area).latitude.max)
        / 2 as libc::c_int as libc::c_double;
    if *xy.offset(1 as libc::c_int as isize) > 85.05112878f64 {
        *xy.offset(1 as libc::c_int as isize) = 85.05112878f64;
    }
    if *xy.offset(1 as libc::c_int as isize) < -85.05112878f64 {
        *xy.offset(1 as libc::c_int as isize) = -85.05112878f64;
    }
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn geohashDecodeToLongLatType(
    hash: GeoHashBits,
    mut xy: *mut libc::c_double,
) -> libc::c_int {
    let mut area: GeoHashArea = {
        let mut init = GeoHashArea {
            hash: {
                let mut init = GeoHashBits {
                    bits: 0 as libc::c_int as uint64_t,
                    step: 0,
                };
                init
            },
            longitude: GeoHashRange { min: 0., max: 0. },
            latitude: GeoHashRange { min: 0., max: 0. },
        };
        init
    };
    if xy.is_null() || geohashDecodeType(hash, &mut area) == 0 {
        return 0 as libc::c_int;
    }
    return geohashDecodeAreaToLongLat(&mut area, xy);
}
#[no_mangle]
pub unsafe extern "C" fn geohashDecodeToLongLatWGS84(
    hash: GeoHashBits,
    mut xy: *mut libc::c_double,
) -> libc::c_int {
    return geohashDecodeToLongLatType(hash, xy);
}
unsafe extern "C" fn geohash_move_x(mut hash: *mut GeoHashBits, mut d: int8_t) {
    if d as libc::c_int == 0 as libc::c_int {
        return;
    }
    let mut x: uint64_t = ((*hash).bits as libc::c_ulonglong
        & 0xaaaaaaaaaaaaaaaa as libc::c_ulonglong) as uint64_t;
    let mut y: uint64_t = ((*hash).bits as libc::c_ulonglong
        & 0x5555555555555555 as libc::c_ulonglong) as uint64_t;
    let mut zz: uint64_t = (0x5555555555555555 as libc::c_ulonglong
        >> 64 as libc::c_int - (*hash).step as libc::c_int * 2 as libc::c_int)
        as uint64_t;
    if d as libc::c_int > 0 as libc::c_int {
        x = x.wrapping_add(zz.wrapping_add(1 as libc::c_int as libc::c_ulong));
    } else {
        x = x | zz;
        x = x.wrapping_sub(zz.wrapping_add(1 as libc::c_int as libc::c_ulong));
    }
    x = (x as libc::c_ulonglong
        & 0xaaaaaaaaaaaaaaaa as libc::c_ulonglong
            >> 64 as libc::c_int - (*hash).step as libc::c_int * 2 as libc::c_int)
        as uint64_t;
    (*hash).bits = x | y;
}
unsafe extern "C" fn geohash_move_y(mut hash: *mut GeoHashBits, mut d: int8_t) {
    if d as libc::c_int == 0 as libc::c_int {
        return;
    }
    let mut x: uint64_t = ((*hash).bits as libc::c_ulonglong
        & 0xaaaaaaaaaaaaaaaa as libc::c_ulonglong) as uint64_t;
    let mut y: uint64_t = ((*hash).bits as libc::c_ulonglong
        & 0x5555555555555555 as libc::c_ulonglong) as uint64_t;
    let mut zz: uint64_t = (0xaaaaaaaaaaaaaaaa as libc::c_ulonglong
        >> 64 as libc::c_int - (*hash).step as libc::c_int * 2 as libc::c_int)
        as uint64_t;
    if d as libc::c_int > 0 as libc::c_int {
        y = y.wrapping_add(zz.wrapping_add(1 as libc::c_int as libc::c_ulong));
    } else {
        y = y | zz;
        y = y.wrapping_sub(zz.wrapping_add(1 as libc::c_int as libc::c_ulong));
    }
    y = (y as libc::c_ulonglong
        & 0x5555555555555555 as libc::c_ulonglong
            >> 64 as libc::c_int - (*hash).step as libc::c_int * 2 as libc::c_int)
        as uint64_t;
    (*hash).bits = x | y;
}
#[no_mangle]
pub unsafe extern "C" fn geohashNeighbors(
    mut hash: *const GeoHashBits,
    mut neighbors: *mut GeoHashNeighbors,
) {
    (*neighbors).east = *hash;
    (*neighbors).west = *hash;
    (*neighbors).north = *hash;
    (*neighbors).south = *hash;
    (*neighbors).south_east = *hash;
    (*neighbors).south_west = *hash;
    (*neighbors).north_east = *hash;
    (*neighbors).north_west = *hash;
    geohash_move_x(&mut (*neighbors).east, 1 as libc::c_int as int8_t);
    geohash_move_y(&mut (*neighbors).east, 0 as libc::c_int as int8_t);
    geohash_move_x(&mut (*neighbors).west, -(1 as libc::c_int) as int8_t);
    geohash_move_y(&mut (*neighbors).west, 0 as libc::c_int as int8_t);
    geohash_move_x(&mut (*neighbors).south, 0 as libc::c_int as int8_t);
    geohash_move_y(&mut (*neighbors).south, -(1 as libc::c_int) as int8_t);
    geohash_move_x(&mut (*neighbors).north, 0 as libc::c_int as int8_t);
    geohash_move_y(&mut (*neighbors).north, 1 as libc::c_int as int8_t);
    geohash_move_x(&mut (*neighbors).north_west, -(1 as libc::c_int) as int8_t);
    geohash_move_y(&mut (*neighbors).north_west, 1 as libc::c_int as int8_t);
    geohash_move_x(&mut (*neighbors).north_east, 1 as libc::c_int as int8_t);
    geohash_move_y(&mut (*neighbors).north_east, 1 as libc::c_int as int8_t);
    geohash_move_x(&mut (*neighbors).south_east, 1 as libc::c_int as int8_t);
    geohash_move_y(&mut (*neighbors).south_east, -(1 as libc::c_int) as int8_t);
    geohash_move_x(&mut (*neighbors).south_west, -(1 as libc::c_int) as int8_t);
    geohash_move_y(&mut (*neighbors).south_west, -(1 as libc::c_int) as int8_t);
}