tarantool 11.0.0

Tarantool rust bindings
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
#![allow(non_camel_case_types)]
//! Module provides FFI bindings for the following constants,
//! types and functios from Tarantool module C API:
//! 1. Clock.
//! 2. COIO.
//! 3. Fibers.
//! 4. Latches.
//! 5. Log.
//! 6. Box - errors, sessions, sequences, transactions, indexes, spaces, tuples.
pub use ::va_list::VaList;

use bitflags::bitflags;
use std::os::raw::{c_char, c_int, c_uint, c_void};

// Clock.
extern "C" {
    pub fn clock_realtime() -> f64;
    pub fn clock_monotonic() -> f64;
    pub fn clock_process() -> f64;
    pub fn clock_thread() -> f64;
    pub fn clock_realtime64() -> u64;
    pub fn clock_monotonic64() -> u64;
    pub fn clock_process64() -> u64;
    pub fn clock_thread64() -> u64;
}

// COIO.
bitflags! {
    /// Event type(s) to wait. Can be `READ` or/and `WRITE`
    pub struct CoIOFlags: c_int {
        const READ = 1;
        const WRITE = 2;
    }
}

extern "C" {
    /// Wait until **READ** or **WRITE** event on socket (`fd`). Yields.
    /// - `fd` - non-blocking socket file description
    /// - `events` - requested events to wait.
    ///   Combination of `TNT_IO_READ` | `TNT_IO_WRITE` bit flags.
    /// - `timeout` - timeout in seconds.
    ///
    /// Returns:
    /// - `0` - timeout
    /// - `>0` - returned events. Combination of `TNT_IO_READ` | `TNT_IO_WRITE` bit flags.
    pub fn coio_wait(fd: c_int, event: c_int, timeout: f64) -> c_int;

    /// Close the fd and wake any fiber blocked in coio_wait() call on this fd.
    pub fn coio_close(fd: c_int) -> c_int;

    /// Fiber-friendly version of getaddrinfo(3).
    ///
    /// - `host` host name, i.e. "tarantool.org"
    /// - `port` service name, i.e. "80" or "http"
    /// - `hints` hints, see getaddrinfo(3)
    /// - `res`(out) result, see getaddrinfo(3)
    /// - `timeout` timeout
    ///
    /// Returns:
    /// -  `0` on success, please free @a res using freeaddrinfo(3).
    /// - `-1` on error, check diag.
    ///
    /// > Please note that the return value is not compatible with getaddrinfo(3).
    pub fn coio_getaddrinfo(
        host: *const c_char,
        port: *const c_char,
        hints: *const libc::addrinfo,
        res: *mut *mut libc::addrinfo,
        timeout: f64,
    ) -> c_int;

    /// Create new eio task with specified function and
    /// arguments. Yield and wait until the task is complete.
    ///
    /// This function doesn't throw exceptions to avoid double error
    /// checking: in most cases it's also necessary to check the return
    /// value of the called function and perform necessary actions. If
    /// func sets errno, the errno is preserved across the call.
    ///
    /// Returns:
    /// - `-1` and `errno = ENOMEM` if failed to create a task
    /// - the function return (errno is preserved).
    ///
    /// # Example
    /// ```c
    /// static ssize_t openfile_cb(va_list ap)
    /// {
    ///      const char *filename = va_arg(ap);
    ///      int flags = va_arg(ap);
    ///      return open(filename, flags);
    /// }
    ///
    /// if (coio_call(openfile_cb, "/tmp/file", 0) == -1)
    ///     // handle errors.
    /// ...
    /// ```
    pub fn coio_call(func: Option<unsafe extern "C" fn(VaList) -> c_int>, ...) -> isize;
}

// Fiber.
#[repr(C)]
pub struct Fiber {
    _unused: [u8; 0],
}

#[repr(C)]
pub struct FiberAttr {
    _unused: [u8; 0],
}

#[repr(C)]
pub struct FiberCond {
    _unused: [u8; 0],
}

pub type FiberFunc = Option<unsafe extern "C" fn(VaList) -> c_int>;

extern "C" {
    /// Return the current fiber
    pub fn fiber_self() -> *mut Fiber;

    /// Create a new fiber.
    ///
    /// Takes a fiber from fiber cache, if it's not empty.
    /// Can fail only if there is not enough memory for
    /// the fiber structure or fiber stack.
    ///
    /// The created fiber automatically returns itself
    /// to the fiber cache when its "main" function
    /// completes.
    ///
    /// - `name`       string with fiber name
    /// - `fiber_func` func for run inside fiber
    ///
    /// See also: [fiber_start](#fn.fiber_start)
    pub fn fiber_new(name: *const c_char, f: FiberFunc) -> *mut Fiber;

    /// Create a new fiber with defined attributes.
    ///
    /// Can fail only if there is not enough memory for
    /// the fiber structure or fiber stack.
    ///
    /// The created fiber automatically returns itself
    /// to the fiber cache if has default stack size
    /// when its "main" function completes.
    ///
    /// - `name`       string with fiber name
    /// - `fiber_attr` fiber attributes
    /// - `fiber_func` func for run inside fiber
    ///
    /// See also: [fiber_start](#fn.fiber_start)
    pub fn fiber_new_ex(
        name: *const c_char,
        fiber_attr: *const FiberAttr,
        f: FiberFunc,
    ) -> *mut Fiber;

    /// Return control to another fiber and wait until it'll be woken.
    ///
    /// See also: [fiber_wakeup](#fn.fiber_wakeup)
    pub fn fiber_yield();

    /// Start execution of created fiber.
    ///
    /// - `callee` fiber to start
    /// - `...`    arguments to start the fiber with
    ///
    /// See also: [fiber_new](#fn.fiber_new)
    pub fn fiber_start(callee: *mut Fiber, ...);

    /// Interrupt a synchronous wait of a fiber. Nop for the currently running
    /// fiber.
    ///
    /// - `f` fiber to be woken up
    pub fn fiber_wakeup(f: *mut Fiber);

    /// Cancel the subject fiber.
    ///
    /// Cancellation is asynchronous. Use fiber_join() to wait for the
    /// cancellation to complete.
    ///
    /// After fiber_cancel() is called, the fiber may or may not check whether
    /// it was cancelled. If the fiber does not check it, it cannot ever be
    /// cancelled. However, as long as most of the cooperative code calls
    /// fiber_testcancel(), most of the fibers are cancellable.
    ///
    /// - `f` fiber to be cancelled
    pub fn fiber_cancel(f: *mut Fiber);

    /// Make it possible or not possible to wakeup the current
    /// fiber immediately when it's cancelled.
    ///
    /// - `yesno` status to set
    ///
    /// Returns: previous state.
    pub fn fiber_set_cancellable(yesno: bool) -> bool;

    /// Set fiber to be joinable (false by default).
    /// - `yesno` status to set
    pub fn fiber_set_joinable(fiber: *mut Fiber, yesno: bool);

    /// Wait until the fiber is dead and then move its execution
    /// status to the caller.
    /// The fiber must not be detached (See also:
    /// [fiber_set_joinable](#fn.fiber_set_joinable)).
    /// `FIBER_IS_JOINABLE` flag is set.
    ///
    /// - `f` fiber to be woken up
    ///
    /// Returns: fiber function ret code
    pub fn fiber_join(f: *mut Fiber) -> c_int;

    /// Put the current fiber to sleep for at least 's' seconds.
    ///
    /// - `s` time to sleep
    ///
    /// **Note:** this is a cancellation point (\sa fiber_is_cancelled)
    pub fn fiber_sleep(s: f64);

    /// Check current fiber for cancellation (it must be checked manually).
    pub fn fiber_is_cancelled() -> bool;

    /// Report loop begin time as double (cheap).
    /// Uses real time clock.
    pub fn fiber_time() -> f64;

    /// Report loop begin time as 64-bit int.
    /// Uses real time clock.
    pub fn fiber_time64() -> u64;

    /// Report loop begin time as double (cheap).
    /// Uses monotonic clock.
    pub fn fiber_clock() -> f64;

    /// Report loop begin time as 64-bit int.
    /// Uses monotonic clock.
    pub fn fiber_clock64() -> u64;

    /// Reschedule fiber to end of event loop cycle.
    pub fn fiber_reschedule();

    /// Create a new fiber attribute container and initialize it
    /// with default parameters.
    /// Can be used for many fibers creation, corresponding fibers
    /// will not take ownership.
    pub fn fiber_attr_new() -> *mut FiberAttr;

    /// Delete the fiber_attr and free all allocated resources.
    /// This is safe when fibers created with this attribute still exist.
    ///
    /// - `fiber_attr` fiber attribute
    pub fn fiber_attr_delete(fiber_attr: *mut FiberAttr);

    /// Set stack size for the fiber attribute.
    ///
    /// - `fiber_attribute` fiber attribute container
    /// - `stacksize` stack size for new fibers
    pub fn fiber_attr_setstacksize(fiber_attr: *mut FiberAttr, stack_size: usize) -> c_int;

    /// Get stack size from the fiber attribute.
    ///
    /// - `fiber_attribute` fiber attribute container or NULL for default
    ///
    /// Returns: stack size
    pub fn fiber_attr_getstacksize(fiber_attr: *mut FiberAttr) -> usize;

    /// Instantiate a new fiber cond object.
    pub fn fiber_cond_new() -> *mut FiberCond;

    /// Delete the fiber cond object.
    /// Behaviour is undefined if there are fiber waiting for the cond.
    pub fn fiber_cond_delete(cond: *mut FiberCond);

    /// Wake one fiber waiting for the cond.
    /// Does nothing if no one is waiting.
    /// - `cond` condition
    pub fn fiber_cond_signal(cond: *mut FiberCond);

    /// Wake up all fibers waiting for the cond.
    /// - `cond` condition
    pub fn fiber_cond_broadcast(cond: *mut FiberCond);

    /// Suspend the execution of the current fiber (i.e. yield) until
    /// fiber_cond_signal() is called. Like pthread_cond, fiber_cond can issue
    /// spurious wake ups caused by explicit fiber_wakeup() or fiber_cancel()
    /// calls. It is highly recommended to wrap calls to this function into a loop
    /// and check an actual predicate and fiber_testcancel() on every iteration.
    ///
    /// - `cond`    condition
    /// - `timeout` timeout in seconds
    ///
    /// Returns:
    /// -  `0` on fiber_cond_signal() call or a spurious wake up
    /// - `-1` on timeout or fiber cancellation, diag is set
    pub fn fiber_cond_wait_timeout(cond: *mut FiberCond, timeout: f64) -> c_int;

    /// Shortcut for fiber_cond_wait_timeout().
    /// See also: [fiber_cond_wait_timeout](#fn.fiber_cond_wait_timeout)
    pub fn fiber_cond_wait(cond: *mut FiberCond) -> c_int;
}

crate::define_extern_or_dlsym_reloc! {
    /// Set fiber name.
    /// - `fiber`: Target fiber, if it's NULL the current fiber is used.
    /// - `name`:  A new name of `fiber`.
    /// - `len`:   Length of the string pointed to by `name`.
    pub fn fiber_set_name_n(fiber: *mut Fiber, name: *const u8, len: u32);

    /// Get fiber name.
    /// - `fiber`: Target fiber, if it's NULL the current fiber is used.
    /// Returns pointer to a nul-terminated string.
    pub fn fiber_name(fiber: *mut Fiber) -> *const u8;

    /// Get fiber id.
    /// - `fiber`: Target fiber, if it's NULL the current fiber is used.
    pub fn fiber_id(fiber: *mut Fiber) -> u64;

    /// Get number of context switches of the given fiber.
    /// - `fiber`: Target fiber, if it's NULL the current fiber is used.
    pub fn fiber_csw(fiber: *mut Fiber) -> u64;

    /// Get a pointer to a live fiber in the current cord by the given fiber id,
    /// which may be used for getting other info about the fiber (name, csw, etc.).
    ///
    /// - `fid` Target fiber id.
    /// Returns fiber on success, NULL if fiber was not found.
    ///
    /// See also [`fiber_name`], [`fiber_csw`], [`fiber_id`]
    pub fn fiber_find(fid: u64) -> *mut Fiber;
}

/// list entry and head structure
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct rlist {
    pub prev: *mut rlist,
    pub next: *mut rlist,
}

/// Channel - fiber communication media.
///
/// A channel is a media to deliver messages between fibers.
/// Any fiber can read or write to/from a channel. Many
/// readers and writers can work with a channel concurrently.
/// A message sent to a channel is read by the first fiber
/// reading from it. If a channel is empty, the reader blocks
/// and waits for a message. If a channel has no reader, the
/// writer waits for the reader to arrive. If a channel is
/// buffered, i.e. has an associated buffer for messages, it
/// is possible for a writer to "drop" the message in a channel
/// until a writer arrives. In case of multiple readers,
/// messages are delivered in FIFO order. In case of multiple
/// writers, the first writer to come is released of its message
/// first.
///
/// If a channel has a buffer of size N, and the buffer
/// is full (contains N messages), and there is a queue of writers,
/// the moment the first reader arrives and reads the first message
/// from a buffer, the first fiber from the wait queue is awoken,
/// and puts its message to the end of the buffer.
///
/// A channel, once created is "open". I.e. anyone can read or
/// write to/from a channel. A channel can be closed at any time,
/// in which case, all messages currently buffered in a channel
/// are destroyed, waiting readers or writers awoken with an error.
///
/// Waiting for a message, a reader, or space in a buffer can also
/// return error in case of a wait timeout or cancellation (when the
/// waiting fiber is cancelled).
///
/// Sending a message to a closed channel, as well as reading
/// a message from such channel, always fails.
///
/// Channel memory layout
/// ---------------------
/// Channel structure has a fixed size. If a channel is created
/// with a buffer, the buffer must be allocated in a continuous
/// memory chunk, directly after the channel itself.
/// fiber_channel_memsize() can be used to find out the amount
/// of memory necessary to store a channel, given the desired
/// buffer size.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct fiber_channel {
    /// Channel buffer size, if the channel is buffered.
    pub size: u32,

    /// The number of messages in the buffer.
    pub count: u32,

    /// Readers blocked waiting for messages while the channel
    /// buffers is empty and/or there are no writers, or
    /// Writers blocked waiting for empty space while the
    /// channel buffer is full and/or there are no readers.
    pub waiters: rlist,

    /// Ring buffer read position.
    pub beg: u32,

    pub is_closed: bool,

    /// Channel buffer, if any.
    pub buf: *mut *mut ipc_msg,
}

/// A base structure for an IPC message.
///
/// A message at any moment can be either:
/// - new
/// - in a channel, waiting to get delivered
/// - delivered
///
/// When a channel is destroyed, all messages buffered by the
/// channel must be destroyed as well. The destroy callback is
/// therefore necessary to free any message-specific resources in
/// case of delivery failure.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ipc_msg {
    pub destroy: Option<unsafe extern "C" fn(*mut ipc_msg)>,
}

pub type ev_tstamp = f64;

/// Infinity is roughly 100 years in seconds.
pub const TIMEOUT_INFINITY: ev_tstamp = 100.0 * 365.0 * 24.0 * 60.0 * 60.0;

crate::define_extern_or_dlsym_reloc! {
    /// Allocate and construct a channel.
    ///
    /// Uses `malloc()`.
    ///
    /// - `size`:  of the channel buffer
    /// - returns: new channel
    ///
    /// ```no_run
    /// use tarantool::ffi::tarantool::fiber_channel_new;
    /// let ch = unsafe { fiber_channel_new(10) };
    /// ```
    pub fn fiber_channel_new(size: u32) -> *mut fiber_channel;

    /// Destroy and free an IPC channel.
    ///
    /// - `ch`: channel
    pub fn fiber_channel_delete(ch: *mut fiber_channel);

    /// Destroy all buffered messages and close the channel.
    ///
    /// - `ch`: channel
    pub fn fiber_channel_close(ch: *mut fiber_channel);

    /// Send a message over a channel within given time.
    ///
    /// - `channel`
    /// - `msg`:  a message with a custom destructor
    /// - `timeout`
    /// **Returns:**
    /// - `0`: success
    /// - `-1`: failure
    ///     - errno=ETIMEDOUT if timeout exceeded,
    ///     - errno=ECANCEL if the fiber is cancelled
    ///     - errno=EBADF if the channel is closed while waiting on it.
    ///
    pub fn fiber_channel_put_msg_timeout(
        ch: *mut fiber_channel,
        msg: *mut ipc_msg,
        timeout: ev_tstamp,
    ) -> c_int;

    /// Get a message from the channel, or time out.
    /// The caller is responsible for message destruction.
    /// **Returns:**
    /// - `0`: success
    /// - `-1`: failure (timeout or channel is closed)
    pub fn fiber_channel_get_msg_timeout(
        ch: *mut fiber_channel,
        msg: *mut *mut ipc_msg,
        timeout: ev_tstamp,
    ) -> c_int;

    /// Check if the channel has reader fibers that wait
    /// for new messages.
    pub fn fiber_channel_has_readers(ch: *mut fiber_channel) -> bool;

    /// Check if the channel has writer fibers that wait
    /// for readers.
    pub fn fiber_channel_has_writers(ch: *mut fiber_channel) -> bool;

    /// Set a pointer to context for the fiber. Can be used to avoid calling
    /// `fiber_start` which means no yields.
    ///
    /// `f`    fiber to set the context for
    /// `ctx`  context for the fiber function
    pub fn fiber_set_ctx(f: *mut Fiber, ctx: *mut c_void);

    /// Get the context for the fiber which was set via the `fiber_set_ctx`
    /// function. Can be used to avoid calling `fiber_start` which means no yields.
    ///
    /// Returns context for the fiber function set by `fiber_set_ctx` function
    ///
    /// See also [`fiber_set_ctx`].
    pub fn fiber_get_ctx(f: *mut Fiber) -> *mut c_void;

}

/// Channel buffer size.
///
/// # Safety
/// `ch` must point to a valid instance of [`fiber_channel`]
#[inline(always)]
pub unsafe fn fiber_channel_size(ch: *mut fiber_channel) -> u32 {
    (*ch).size
}

/// The number of messages in the buffer.
/// There may be more messages outstanding
/// if the buffer is full.
///
/// # Safety
/// `ch` must point to a valid instance of [`fiber_channel`]
#[inline(always)]
pub unsafe fn fiber_channel_count(ch: *mut fiber_channel) -> u32 {
    (*ch).count
}

/// True if the channel is closed for both for reading
/// and writing.
///
/// # Safety
/// `ch` must point to a valid instance of [`fiber_channel`]
#[inline(always)]
pub unsafe fn fiber_channel_is_closed(ch: *mut fiber_channel) -> bool {
    (*ch).is_closed
}

/// # Safety
/// `ch` must point to a valid instance of [`fiber_channel`]
#[inline(always)]
pub unsafe fn fiber_channel_is_empty(ch: *mut fiber_channel) -> bool {
    (*ch).count == 0
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ipc_data {
    pub data: *mut c_void,
    pub i: c_int,
}

/// A message implementation to pass simple value across
/// a channel.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ipc_value {
    pub base: ipc_msg,
    pub data_union: ipc_data,
}

crate::define_dlsym_reloc! {
    pub fn ipc_value_new() -> *mut ipc_value;
    pub fn ipc_value_delete(msg: *mut ipc_msg);

    /// Try to lock a latch. Return immediately if the latch is locked.
    /// - `latch` a latch
    ///
    /// Returns:
    /// - `0` - success
    /// - `1` - the latch is locked.
    pub fn box_latch_lock_timeout(latch: *mut Latch, timeout: ev_tstamp) -> c_int;
}

// Latch.
#[repr(C)]
pub struct Latch {
    _unused: [u8; 0],
}

extern "C" {
    /// Allocate and initialize the new latch.
    ///
    /// Returns: latch
    pub fn box_latch_new() -> *mut Latch;

    /// Destroy and free the latch.
    /// - `latch` latch
    pub fn box_latch_delete(latch: *mut Latch);

    /// Lock a latch. Waits indefinitely until the current fiber can gain access to
    /// the latch.
    ///
    /// - `latch` a latch
    pub fn box_latch_lock(latch: *mut Latch);

    /// Try to lock a latch. Return immediately if the latch is locked.
    /// - `latch` a latch
    ///
    /// Returns:
    /// - `0` - success
    /// - `1` - the latch is locked.
    pub fn box_latch_trylock(latch: *mut Latch) -> c_int;

    /// Unlock a latch. The fiber calling this function must
    /// own the latch.
    ///
    /// - `latch` a latch
    pub fn box_latch_unlock(latch: *mut Latch);
}

// Log.
pub type SayFunc =
    Option<unsafe extern "C" fn(c_int, *const c_char, c_int, *const c_char, *const c_char, ...)>;

extern "C" {
    #[link_name = "log_level"]
    pub static mut LOG_LEVEL: c_int;

    #[link_name = "_say"]
    pub static mut SAY_FN: SayFunc;

    /// Set log level for the default logger. Can be used dynamically.
    pub fn say_set_log_level(level: c_int) -> c_void;
}

// Tarantool log object.
#[cfg(feature = "picodata")]
#[repr(C)]
pub struct Logger {
    _unused: [u8; 0],
}

#[cfg(feature = "picodata")]
pub type LogFormatFn = unsafe extern "C" fn(
    log: *const c_void,
    buf: *const c_char,
    len: c_int,
    level: c_int,
    module: *const c_char,
    filename: *const c_char,
    line: c_int,
    error: *const c_char,
    format: *const c_char,
    ap: VaList,
) -> c_int;

#[cfg(feature = "picodata")]
extern "C" {
    pub fn log_set_format(log: *mut Logger, format: LogFormatFn) -> c_void;
    pub fn log_default_logger() -> *mut Logger;
}

/// A tarantool error payload type. Corresponds to `struct error_payload` on the C side.
#[repr(C)]
pub struct BoxErrorPayload {
    _unused: [u8; 0],
}

/// A tarantool error type. Corresponds to `struct error` on the C side.
#[repr(C)]
pub struct BoxError {
    _unused: [u8; 0],
}

extern "C" {
    /// Return IPROTO error code
    /// - `error` error
    ///
    /// Returns: enum `box_error_code`
    pub fn box_error_code(error: *const BoxError) -> u32;

    /// Return the error message
    /// - `error` error
    ///
    /// Returns: not-null string
    pub fn box_error_message(error: *const BoxError) -> *const c_char;

    /// Get the information about the last API call error.
    ///
    /// The Tarantool error handling works most like libc's errno. All API calls
    /// return -1 or NULL in the event of error. An internal pointer to
    /// box_error_t type is set by API functions to indicate what went wrong.
    /// This value is only significant if API call failed (returned -1 or NULL).
    ///
    /// Successful function can also touch the last error in some
    /// cases. You don't have to clear the last error before calling
    /// API functions. The returned object is valid only until next
    /// call to **any** API function.
    ///
    /// You must set the last error using [box_error_set](#fn.box_error_set) in
    /// your stored C procedures if you want to return a custom error message.
    /// You can re-throw the last API error to IPROTO client by keeping
    /// the current value and returning -1 to Tarantool from your
    /// stored procedure.
    ///
    /// Returns: last error.
    pub fn box_error_last() -> *mut BoxError;

    /// Return the error type, e.g. "ClientError", "SocketError", etc.
    /// - `error`
    ///
    /// Returns: not-null string
    pub fn box_error_type(error: *const BoxError) -> *const c_char;

    /// Clear the last error.
    pub fn box_error_clear();

    /// Set the last error.
    ///
    /// - `code` IPROTO error code (enum \link box_error_code \endlink)
    /// - `format` (const char * ) - printf()-like format string
    /// - ... - format arguments
    ///
    /// Returns: `-1` for convention use
    pub fn box_error_set(
        file: *const c_char,
        line: c_uint,
        code: u32,
        format: *const c_char,
        ...
    ) -> c_int;

    /// Increase reference counter of `error`.
    ///
    /// Note that technically this function takes a pointer to `struct error`
    /// while the other `box_error_*` functions take a `box_error_t`. Internally
    /// `box_error_t` is defined as an alias to `struct error *` so this should
    /// be correct and there's no reason for this to change. But technically
    /// it's possible that tarantool's developers will break this compatibility
    /// and make `box_error_t` be something else, so it will no longer be safe
    /// to call this function with a pointer to `BoxError`.
    pub fn error_ref(error: *mut BoxError);

    /// Decrease reference counter of `error`.
    /// If it reaches `0`, the error is destroyed.
    ///
    /// Note that technically this function takes a pointer to `struct error`
    /// while the other `box_error_*` functions take a `box_error_t`. Internally
    /// `box_error_t` is defined as an alias to `struct error *` so this should
    /// be correct and there's no reason for this to change. But technically
    /// it's possible that tarantool's developers will break this compatibility
    /// and make `box_error_t` be something else, so it will no longer be safe
    /// to call this function with a pointer to `BoxError`.
    pub fn error_unref(error: *mut BoxError);
}

/// An iterator over [`BoxErrorPayload`] fields. Corresponds to `struct error_payload_iter` on the C side.
#[cfg(feature = "picodata")]
#[derive(Default)]
#[repr(C)]
pub struct BoxErrorPayloadIter {
    /// Index of the next field to be returned.
    pub next_position: c_int,
    /// Zero terminated field name.
    pub name: *const c_char,
    /// MessagePack field value.
    pub mp_value: *const c_char,
    /// Size of MessagePack field value.
    pub mp_size: usize,
}

#[cfg(feature = "picodata")]
extern "C" {
    /// Gets the pointer to error payload from error pointer.
    pub fn error_get_payload(error: *mut BoxError) -> *mut BoxErrorPayload;

    /// Iterate over fields stored in the error payload.
    ///
    /// To start iteration, construct a `error_payload_iter`
    /// setting `next_position` to 0 and pass it to this function.
    ///
    /// Returns `true` if a new field information returned by this function.
    /// Returns `false` if there are no more fields.
    ///
    /// Make sure to not modify the error payload object while it's
    /// being iterated on.
    pub fn error_payload_iter_next(
        payload: *const BoxErrorPayload,
        iter: &mut BoxErrorPayloadIter,
    ) -> bool;

    /// Get MessagePack value of a payload field. If it is not found - return NULL
    /// and the out parameter is set to 0.
    pub fn error_payload_get_mp(
        payload: *const BoxErrorPayload,
        name: *const c_char,
        size: &mut u32,
    ) -> *const c_char;

    /// Set value of a payload field to a MessagePack buffer. If the field existed
    /// before, it is overwritten.
    pub fn error_payload_set_mp(
        payload: *mut BoxErrorPayload,
        name: *const c_char,
        src: *const c_char,
        size: u32,
    );
}

crate::define_extern_or_dlsym_reloc! {
    #[allow(non_snake_case)]
    pub fn luaL_iserror(l: *mut lua_State, narg: std::ffi::c_int) -> *mut BoxError;
}

// Session.
extern "C" {
    pub fn box_session_push(data: *const c_char, data_end: *const c_char) -> c_int;
}

// Sequence.
extern "C" {
    pub fn box_sequence_next(seq_id: u32, result: *mut i64) -> c_int;
    pub fn box_sequence_set(seq_id: u32, value: i64) -> c_int;
    pub fn box_sequence_reset(seq_id: u32) -> c_int;
}

// Transaction.
extern "C" {
    pub fn box_txn() -> bool;
    pub fn box_txn_begin() -> c_int;
    pub fn box_txn_commit() -> c_int;
    pub fn box_txn_rollback() -> c_int;
    pub fn box_txn_alloc(size: usize) -> *mut c_void;
}

// Indexes, spaces and tuples.
pub const BOX_ID_NIL: u32 = 2147483647;

extern "C" {
    pub fn box_insert(
        space_id: u32,
        tuple: *const c_char,
        tuple_end: *const c_char,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_update(
        space_id: u32,
        index_id: u32,
        key: *const c_char,
        key_end: *const c_char,
        ops: *const c_char,
        ops_end: *const c_char,
        index_base: c_int,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_upsert(
        space_id: u32,
        index_id: u32,
        tuple: *const c_char,
        tuple_end: *const c_char,
        ops: *const c_char,
        ops_end: *const c_char,
        index_base: c_int,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_replace(
        space_id: u32,
        tuple: *const c_char,
        tuple_end: *const c_char,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_delete(
        space_id: u32,
        index_id: u32,
        key: *const c_char,
        key_end: *const c_char,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_truncate(space_id: u32) -> c_int;
}

extern "C" {
    /// Try to look up a space by space number in the space cache.
    /// FFI-friendly no-exception-thrown space lookup function.
    ///
    /// Return NULL if space not found, otherwise space object.
    ///
    /// # Safety
    /// The caller must make sure not to hold on to the pointer for too
    /// long as the space object may get deleted at some point after which
    /// the derefencing the pointer will be **undefined behavior**.
    pub(crate) fn space_by_id(id: u32) -> *mut space;

    /// Returns number of bytes used in memory by tuples in the space.
    pub(crate) fn space_bsize(space: *mut space) -> usize;
}

#[repr(C)]
pub(crate) struct space {
    unused: [u8; 0],
}

////////////////////////////////////////////////////////////////////////////////
// ...
////////////////////////////////////////////////////////////////////////////////

extern "C" {
    pub fn box_index_id_by_name(space_id: u32, name: *const c_char, len: u32) -> u32;
    pub fn box_space_id_by_name(name: *const c_char, len: u32) -> u32;
    pub fn box_index_len(space_id: u32, index_id: u32) -> isize;
    pub fn box_index_bsize(space_id: u32, index_id: u32) -> isize;
    pub fn box_index_random(
        space_id: u32,
        index_id: u32,
        rnd: u32,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_index_get(
        space_id: u32,
        index_id: u32,
        key: *const c_char,
        key_end: *const c_char,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_index_min(
        space_id: u32,
        index_id: u32,
        key: *const c_char,
        key_end: *const c_char,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_index_max(
        space_id: u32,
        index_id: u32,
        key: *const c_char,
        key_end: *const c_char,
        result: *mut *mut BoxTuple,
    ) -> c_int;
    pub fn box_index_count(
        space_id: u32,
        index_id: u32,
        type_: c_int,
        key: *const c_char,
        key_end: *const c_char,
    ) -> isize;
}

#[repr(C)]
pub struct BoxIterator {
    _unused: [u8; 0],
}

// Index iterator
extern "C" {
    pub fn box_index_iterator(
        space_id: u32,
        index_id: u32,
        type_: c_int,
        key: *const c_char,
        key_end: *const c_char,
    ) -> *mut BoxIterator;
    pub fn box_iterator_next(iterator: *mut BoxIterator, result: *mut *mut BoxTuple) -> c_int;
    pub fn box_iterator_free(iterator: *mut BoxIterator);
}

/// Analogue of tarantool's `box_tuple_t`
#[cfg(not(feature = "picodata"))]
#[repr(C)]
pub struct BoxTuple {
    refs: u16,
    format_id: u16,
    bsize: u32,
    data_offset: u16,
}

#[cfg(feature = "picodata")]
#[repr(C, packed)]
pub struct BoxTuple {
    pub refs: u8,
    pub(crate) flags: u8,
    pub(crate) format_id: u16,
    pub(crate) data_offset: u16,
    pub(crate) bsize: u32,
}

#[cfg(not(feature = "picodata"))]
impl BoxTuple {
    /// # Safety
    /// Access to a field of a struct that can be changed in a future version of
    /// tarantool. Valid for 2.9.0
    #[inline(always)]
    pub unsafe fn data_offset(&self) -> u16 {
        // The last bit is a `is_dirty` flag since 2.5.1
        self.data_offset & (u16::MAX >> 1)
    }
}

#[cfg(feature = "picodata")]
impl BoxTuple {
    /// # Safety
    /// Access to a field of a struct that can be changed in a future version of
    /// tarantool. Valid for 2.10.0
    #[inline(always)]
    pub unsafe fn data_offset(&self) -> u16 {
        box_tuple_data_offset(self)
    }
}

impl BoxTuple {
    pub fn bsize(&self) -> usize {
        unsafe { box_tuple_bsize(self) }
    }

    /// # Safety
    /// This is how tuple data is stored in tarantool.
    #[inline]
    pub unsafe fn data(&self) -> &[u8] {
        unsafe {
            let data_offset = self.data_offset() as isize;
            let data = (self as *const BoxTuple).cast::<u8>().offset(data_offset);
            std::slice::from_raw_parts(data, self.bsize())
        }
    }
}

#[cfg(feature = "picodata")]
pub(crate) type FnTupleDelete =
    unsafe extern "C" fn(format: *mut BoxTupleFormat, tuple: *mut BoxTuple);

#[cfg(feature = "picodata")]
pub(crate) type FnTupleNew = unsafe extern "C" fn(
    format: *mut BoxTupleFormat,
    data: *const u8,
    end: *const u8,
) -> *mut BoxTuple;

#[cfg(feature = "picodata")]
#[repr(C)]
pub(crate) struct FormatVTable {
    pub(crate) tuple_delete: FnTupleDelete,
    pub(crate) tuple_new: FnTupleNew,
}

#[cfg(feature = "picodata")]
#[repr(C)]
pub(crate) struct TupleDictionary {
    _hash: *const c_void,
    pub(crate) names: *const *const c_char,
    pub(crate) name_count: u32,
    _refs: c_int,
}

#[cfg(feature = "picodata")]
#[repr(C)]
pub struct BoxTupleFormat {
    pub(crate) vtab: FormatVTable,
    _engine: *const c_void,
    pub(crate) id: u16,
    _hash: u32,
    _epoch: u64,
    pub(crate) refs: c_int,
    _is_temporary: bool,
    _is_reusable: bool,
    _is_compressed: bool,
    _field_map_size: u16,
    _exact_field_count: u32,
    _index_field_count: u32,
    _min_field_count: u32,
    _total_field_count: u32,
    _required_fields: *const c_void,
    pub(crate) dict: *const TupleDictionary,
}

#[cfg(not(feature = "picodata"))]
#[repr(C)]
pub struct BoxTupleFormat {
    _unused: [u8; 0],
}

// Tuple
extern "C" {
    pub fn box_tuple_extract_key(
        tuple: *const BoxTuple,
        space_id: u32,
        index_id: u32,
        key_size: *mut u32,
    ) -> *mut c_char;
    pub fn box_tuple_new(
        format: *mut BoxTupleFormat,
        data: *const c_char,
        end: *const c_char,
    ) -> *mut BoxTuple;
    pub fn box_tuple_ref(tuple: *mut BoxTuple) -> c_int;
    pub fn box_tuple_unref(tuple: *mut BoxTuple);
    pub fn box_tuple_field_count(tuple: *const BoxTuple) -> u32;
    pub fn box_tuple_bsize(tuple: *const BoxTuple) -> usize;
    #[cfg(feature = "picodata")]
    pub fn box_tuple_data_offset(tuple: *const BoxTuple) -> u16;
    pub fn box_tuple_to_buf(tuple: *const BoxTuple, buf: *mut c_char, size: usize) -> isize;
    pub fn box_tuple_format_default() -> *mut BoxTupleFormat;
    pub fn box_tuple_format(tuple: *const BoxTuple) -> *mut BoxTupleFormat;

    /// Increment tuple format ref count.
    ///
    /// - `tuple_format` the tuple format to ref
    pub fn box_tuple_format_ref(format: *mut BoxTupleFormat);

    /// Decrement tuple format ref count.
    ///
    /// - `tuple_format` the tuple format to unref
    pub fn box_tuple_format_unref(format: *mut BoxTupleFormat);

    /// Return new in-memory tuple format based on passed key definitions.
    ///
    /// The returned format has one reference.
    ///
    /// - `keys` array of keys defined for the format
    /// - `key_count` count of keys
    ///
    /// Returns
    /// - new tuple format if success
    /// - `NULL` for error
    pub fn box_tuple_format_new(keys: *mut *mut BoxKeyDef, key_count: u16) -> *mut BoxTupleFormat;

    pub fn box_tuple_field(tuple: *const BoxTuple, fieldno: u32) -> *const c_char;
    pub fn box_tuple_compare(
        tuple_a: *mut BoxTuple,
        tuple_b: *mut BoxTuple,
        key_def: *mut BoxKeyDef,
    ) -> c_int;
    pub fn box_tuple_compare_with_key(
        tuple_a: *mut BoxTuple,
        key_b: *const c_char,
        key_def: *mut BoxKeyDef,
    ) -> c_int;
    #[cfg(feature = "picodata")]
    pub fn box_tuple_hash(tuple: *mut BoxTuple, key_def: *mut BoxKeyDef) -> c_uint;
}

pub(crate) const TUPLE_FIELD_BY_PATH_OLD_API: &str = "tuple_field_raw_by_full_path\0";
pub(crate) const TUPLE_FIELD_BY_PATH_NEW_API: &str = "box_tuple_field_by_path\0";

#[repr(C)]
pub struct BoxTupleIterator {
    _unused: [u8; 0],
}

// Tuple iterator
extern "C" {
    pub fn box_tuple_iterator(tuple: *mut BoxTuple) -> *mut BoxTupleIterator;
    pub fn box_tuple_iterator_free(it: *mut BoxTupleIterator);
    pub fn box_tuple_position(it: *mut BoxTupleIterator) -> u32;
    pub fn box_tuple_rewind(it: *mut BoxTupleIterator);
    pub fn box_tuple_seek(it: *mut BoxTupleIterator, fieldno: u32) -> *const c_char;
    pub fn box_tuple_next(it: *mut BoxTupleIterator) -> *const c_char;
}

// Function
extern "C" {
    #[cfg(feature = "picodata")]
    /// Generate unique id for a function.
    pub fn box_generate_func_id(new_func_id: *mut u32, use_reserved_range: bool) -> c_int;
}

////////////////////////////////////////////////////////////////////////////////
// box_key_def_t
////////////////////////////////////////////////////////////////////////////////

/// Analogue of tarantool's `box_key_def_t`.
#[repr(C)]
pub struct BoxKeyDef {
    _unused: [u8; 0],
}

#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BoxKeyDefPart {
    /// Index of a tuple field (zero based).
    pub fieldno: u32,

    /// Flags, e.g. nullability.
    pub flags: u32,

    /// Type of the tuple field.
    pub field_type: *const c_char,

    /// Collation name for string comparisons.
    pub collation: *const c_char,

    /// JSON path to point a nested field.
    ///
    /// Example:
    /// ```ignore
    /// tuple: [1, {"foo": "bar"}]
    /// key parts: [
    ///     {
    ///         "fieldno": 2,
    ///         "type": "string",
    ///         "path": "foo"
    ///     }
    /// ]
    ///
    /// => key: ["bar"]
    /// ```
    ///
    /// Note: When the path is given, <field_type>
    /// means type of the nested field.
    pub path: *const c_char,
}

const BOX_KEY_PART_DEF_T_SIZE: usize = 64;

#[repr(C)]
pub union box_key_part_def_t {
    pub meat: BoxKeyDefPart,
    /// Padding to guarantee certain size across different
    /// tarantool versions.
    _padding: [u8; BOX_KEY_PART_DEF_T_SIZE],
}

bitflags! {
    /// Key part definition flag.
    pub struct BoxKeyDefPartFlag: u32 {
        const IS_NULLABLE = 1 << 0;
    }
}

extern "C" {
    pub fn box_key_def_new(fields: *mut u32, types: *mut u32, part_count: u32) -> *mut BoxKeyDef;
    pub fn box_key_def_new_v2(parts: *mut box_key_part_def_t, part_count: u32) -> *mut BoxKeyDef;
    pub fn box_key_def_delete(key_def: *mut BoxKeyDef);

    /// Check that tuple fields match with given key definition.
    ///
    /// Arguments:
    /// `key_def`  Key definition.
    /// `tuple`    Tuple to validate.
    ///
    /// Returns:
    /// - `0`   The tuple is valid.
    /// - `-1`  The tuple is invalid.
    ///
    /// In case of an invalid tuple set a diag and return -1.
    /// See also [`box_error_last`]
    pub fn box_key_def_validate_tuple(key_def: *mut BoxKeyDef, tuple: *mut BoxTuple) -> c_int;

    /// Extract key from tuple by given key definition and return
    /// buffer allocated on the box region with this key.
    /// See also [`box_region_truncate`]
    ///
    /// This function has _O(n)_ complexity, where _n_ is the number of key
    /// parts.
    ///
    /// Arguments:
    /// `key_def`       Definition of key that need to extract.
    /// `tuple`         Tuple from which need to extract key.
    /// `multikey_idx`  Multikey index hint or `-1`.
    /// `key_size_ptr`  Here will be size of extracted key.
    ///
    /// Returns:
    /// - not `NULL`  Success.
    /// - `NULL`      Memory allocation error.
    ///
    /// In case of an error set a diag and return `NULL`.
    /// See also [`box_error_last`]
    pub fn box_key_def_extract_key(
        key_def: *mut BoxKeyDef,
        tuple: *mut BoxTuple,
        multikey_idx: c_int,
        key_size_ptr: *mut u32,
    ) -> *mut c_char;

    /// Check a key against given key definition.
    ///
    /// Verifies key parts against given key_def's field types with
    /// respect to nullability.
    ///
    /// A partial key (with less part than defined in @a key_def) is
    /// verified by given key parts, the omitted tail is not verified
    /// anyhow.
    ///
    /// Note: nil is accepted for nullable fields, but only for them.
    ///
    /// Arguments:
    /// `key_def`       Key definition.
    /// `key`           MessagePack'ed data for matching.
    /// `key_size_ptr`  Here will be size of the validated key.
    ///
    /// Returns:
    /// - `0`   The key is valid.
    /// - `-1`  The key is invalid.
    ///
    /// In case of an invalid key set a diag and return -1.
    /// See also [`box_error_last`]
    pub fn box_key_def_validate_key(
        key_def: *const BoxKeyDef,
        key: *const c_char,
        key_size_ptr: *mut u32,
    ) -> c_int;

    /// Check a full key against given key definition.
    ///
    /// Verifies key parts against given key_def's field types with
    /// respect to nullability.
    ///
    /// Imposes the same parts count in @a key as in @a key_def.
    /// Absence of trailing key parts fails the check.
    ///
    /// Note: nil is accepted for nullable fields, but only for them.
    ///
    /// Arguments:
    /// `key_def`       Key definition.
    /// `key`           MessagePack'ed data for matching.
    /// `key_size_ptr`  Here will be size of the validated key.
    ///
    /// Returns:
    /// - `0`   The key is valid.
    /// - `-1`  The key is invalid.
    ///
    /// In case of an invalid key set a diag and return -1.
    /// See also [`box_error_last`]
    pub fn box_key_def_validate_full_key(
        key_def: *const BoxKeyDef,
        key: *const c_char,
        key_size_ptr: *mut u32,
    ) -> c_int;
}

////////////////////////////////////////////////////////////////////////////////
// box_region
////////////////////////////////////////////////////////////////////////////////

extern "C" {
    /// How much memory is used by the box region.
    pub fn box_region_used() -> usize;

    /// Allocate size bytes from the box region.
    ///
    /// Don't use this function to allocate a memory block for a value
    /// or array of values of a type with alignment requirements. A
    /// violation of alignment requirements leads to undefined
    /// behaviour.
    ///
    /// In case of a memory error set a diag and return NULL.
    /// See also [`box_error_last`].
    pub fn box_region_alloc(size: usize) -> *mut c_void;

    /// Allocate size bytes from the box region with given alignment.
    ///
    /// Alignment must be a power of 2.
    ///
    /// In case of a memory error set a diag and return NULL.
    /// See also [`box_error_last`].
    pub fn box_region_aligned_alloc(size: usize, alignment: usize) -> *mut c_void;

    /// Truncate the box region to the given size.
    pub fn box_region_truncate(size: usize);
}

////////////////////////////////////////////////////////////////////////////////
// ...
////////////////////////////////////////////////////////////////////////////////

#[repr(C)]
pub struct BoxFunctionCtx {
    #[cfg(not(feature = "picodata"))]
    _unused: [u8; 0],
    #[cfg(feature = "picodata")]
    pub(crate) port: *mut crate::ffi::sql::Port,
}

extern "C" {
    /// Return a tuple from stored C procedure.
    ///
    /// Returned tuple is automatically reference counted by Tarantool.
    ///
    /// `ctx`: An opaque structure passed to the stored C procedure by
    /// Tarantool
    /// `tuple`: A tuple to return
    /// Returns:
    /// - `-1` on error (perhaps, out of memory; check box_error_last())
    /// - `0` otherwise
    pub fn box_return_tuple(ctx: *mut BoxFunctionCtx, tuple: *mut BoxTuple) -> c_int;

    /// Return MessagePack from a stored C procedure. The MessagePack
    /// is copied, so it is safe to free/reuse the passed arguments
    /// after the call.
    /// MessagePack is not validated, for the sake of speed. It is
    /// expected to be a single encoded object. An attempt to encode
    /// and return multiple objects without wrapping them into an
    /// MP_ARRAY or MP_MAP is undefined behaviour.
    ///
    /// `ctx`: An opaque structure passed to the stored C procedure by
    /// Tarantool.
    /// `mp`: Begin of MessagePack.
    /// `mp_end`: End of MessagePack.
    /// Returns:
    /// - `-1` Error.
    /// - `0` Success.
    pub fn box_return_mp(
        ctx: *mut BoxFunctionCtx,
        mp: *const c_char,
        mp_end: *const c_char,
    ) -> c_int;
}

use crate::ffi::lua::lua_State;

extern "C" {
    pub fn luaT_state() -> *mut lua_State;
    pub fn luaT_call(l: *mut lua_State, nargs: c_int, nreturns: c_int) -> isize;
    pub fn luaT_istuple(l: *mut lua_State, index: i32) -> *mut BoxTuple;
    pub fn luaT_pushtuple(l: *mut lua_State, tuple: *mut BoxTuple);
    pub fn luaT_tuple_encode(l: *mut lua_State, index: i32, len: *mut usize) -> *const u8;
    pub fn luaT_tuple_new(
        l: *mut lua_State,
        index: i32,
        format: *mut BoxTupleFormat,
    ) -> *mut BoxTuple;
}

extern "C" {
    /// Function, which registers or deletes on_shutdown handler.
    /// - `arg` on_shutdown function's argument.
    /// - `new_handler` New on_shutdown handler, in case this argument is `NULL`,
    ///   function finds and destroys old on_shutdown handler.
    /// - `old_handler` Old on_shutdown handler.
    ///
    /// Returns 0 if success otherwise return -1 and sets errno.
    /// There are three cases when function fails:
    ///    - both old_handler and new_handler are equal to
    ///      zero (sets errno to EINVAL).
    ///    - old_handler != NULL, but there is no trigger
    ///      with such function (sets errno to EINVAL).
    ///    - malloc for some internal struct memory allocation
    ///      return NULL (errno sets by malloc to ENOMEM).
    pub fn box_on_shutdown(
        arg: *mut c_void,
        new_handler: Option<extern "C" fn(*mut c_void) -> c_int>,
        old_handler: Option<extern "C" fn(*mut c_void) -> c_int>,
    ) -> c_int;
}

/// Tarantool stored procedure signature.
pub type Proc =
    unsafe extern "C" fn(crate::tuple::FunctionCtx, crate::tuple::FunctionArgs) -> c_int;

// Cbus lcpipe.
#[cfg(feature = "picodata")]
#[repr(C)]
pub struct LCPipe {
    _unused: [u8; 0],
}

#[cfg(feature = "picodata")]
extern "C" {
    pub fn lcpipe_new(name: *const c_char) -> *mut LCPipe;
    pub fn lcpipe_push_now(lcpipe: *mut LCPipe, cmsg: *mut c_void);
    pub fn lcpipe_delete(lcpipe: *mut LCPipe);
    pub fn cbus_endpoint_new(endpoint: *mut *mut c_void, name: *const c_char) -> c_int;
    pub fn cbus_endpoint_delete(endpoint: *mut c_void) -> c_int;
    pub fn cbus_loop(endpoint: *mut c_void);
    pub fn cbus_process(endpoint: *mut c_void);
}

// Schema.
extern "C" {
    /// Returns the current version of the database schema.
    pub fn box_schema_version() -> u64;
}

// Session.
#[cfg(feature = "picodata")]
extern "C" {
    /// Returns the unique identifier (ID) for the current session.
    pub fn box_session_id() -> u64;
    pub fn box_session_user_id(uid: *mut u32) -> c_int;
    pub fn box_effective_user_id() -> u32;
    pub fn box_session_su(uid: u32) -> c_int;
    pub fn box_user_id_by_name(
        name: *const c_char,
        name_end: *const c_char,
        uid: *mut u32,
    ) -> c_int;
}

// Authentication.
#[cfg(feature = "picodata")]
extern "C" {
    pub fn box_auth_data_prepare(
        method_name: *const c_char,
        method_name_end: *const c_char,
        password: *const c_char,
        password_end: *const c_char,
        user_name: *const c_char,
        user_name_end: *const c_char,
        data: *const *const c_char,
        data_end: *const *const c_char,
    ) -> c_int;
}

////////////////////////////////////////////////////////////////////////////////
// box_read_view
////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "picodata")]
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct box_read_view_t {
    _unused: [u8; 0],
}

#[cfg(feature = "picodata")]
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct box_read_view_iterator_t {
    _unused: [u8; 0],
}

#[cfg(feature = "picodata")]
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct space_index_id {
    pub space_id: u32,
    pub index_id: u32,
}

#[cfg(feature = "picodata")]
extern "C" {
    pub fn box_read_view_open_for_given_spaces(
        name: *const std::ffi::c_char,
        space_index_ids: *const space_index_id,
        space_index_ids_count: u32,
        flags: u64,
    ) -> *mut box_read_view_t;
    pub fn box_read_view_close(rv: *mut box_read_view_t);
    pub fn box_read_view_iterator_all(
        rv: *mut box_read_view_t,
        space_id: u32,
        index_id: u32,
        iter: *mut *mut box_read_view_iterator_t,
    ) -> i32;
    pub fn box_read_view_iterator_next_raw(
        iter: *mut box_read_view_iterator_t,
        data: *mut *const u8,
        size: *mut u32,
    ) -> i32;
    pub fn box_read_view_iterator_free(iter: *mut box_read_view_iterator_t);
}

// Access control.
#[cfg(feature = "picodata")]
extern "C" {
    pub fn box_access_check_space(space_id: u32, user_access: u16) -> c_int;
    pub fn box_access_check_ddl(
        name: *const c_char,
        object_id: u32,
        owner_id: u32,
        object_type: u32,
        access: u16,
    ) -> c_int;
}

// Cord.
#[cfg(feature = "picodata")]
extern "C" {
    pub fn current_cord_name() -> *const c_char;
    pub fn cord_is_main() -> bool;
    pub fn cord_is_main_dont_create() -> bool;
}

#[cfg(feature = "picodata")]
#[cfg(feature = "internal_test")]
mod tests {
    use super::*;
    use crate::log::SayLevel;
    use std::ffi::CStr;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::thread;

    #[crate::test(tarantool = "crate")]
    pub fn test_set_log_format() {
        static LOG_FORMAT_CALLED: AtomicBool = AtomicBool::new(false);

        extern "C" fn flag_trigger_format(
            _: *const c_void,
            _: *const c_char,
            _: c_int,
            _: c_int,
            _: *const c_char,
            _: *const c_char,
            _: c_int,
            _: *const c_char,
            _: *const c_char,
            _: VaList,
        ) -> c_int {
            LOG_FORMAT_CALLED.store(true, Ordering::SeqCst);
            0
        }

        let default_logger = unsafe { log_default_logger() };
        unsafe { log_set_format(default_logger, flag_trigger_format) };

        crate::log::say(SayLevel::Error, "", 0, None, "test log");

        assert!(LOG_FORMAT_CALLED.load(Ordering::SeqCst));
    }

    #[crate::test(tarantool = "crate")]
    pub fn test_cord_info_functions() {
        assert!(unsafe { cord_is_main() });
        assert!(unsafe { cord_is_main_dont_create() });
        let cord_name_ptr = unsafe { current_cord_name() };
        let cord_name = unsafe { CStr::from_ptr(cord_name_ptr) }.to_string_lossy();
        assert_eq!(cord_name, "main");

        let thread = thread::spawn(|| {
            let cord_name_ptr = unsafe { current_cord_name() };
            assert!(cord_name_ptr.is_null());
            assert!(!unsafe { cord_is_main_dont_create() });
            assert!(!unsafe { cord_is_main() });
        });
        thread.join().unwrap();
    }
}