@@ -228,6 +228,50 @@ pub mod types {
228
228
}
229
229
230
230
// Standard types that are scalar but vary by OS and arch.
231
+ //
232
+ // ISO C's `size_t` is defined to be the type of the result of the
233
+ // `sizeof` operator and the type of the size parameter to `malloc`. That
234
+ // is, C's `size_t` is only required to hold the size of the largest object
235
+ // that can be allocated. In particular, it is legal for a C implementation
236
+ // to have a maximum object size smaller than the entire address space. For
237
+ // example, a C implementation may have an maximum object size of 2^32
238
+ // bytes with a 64-bit address space, and typedef `size_t` as `uint32_t` so
239
+ // that `sizeof(size_t) == 4` and `sizeof(void*) == 8`.
240
+ //
241
+ // Rust's `usize`, on the other hand, is defined to always be the same size
242
+ // as a pointer. This means that it is possible, in theory, to have a
243
+ // platform where `usize` can represent values that `size_t` cannot
244
+ // represent. However, on the vast majority of systems, `usize` and
245
+ // `size_t` are represented the same way. If it were required to explicitly
246
+ // cast `usize` to `size_t` on common platforms, then many programmers
247
+ // would habitually write expressions such as
248
+ // `my_slice.len() as libc::size_t` expecting this to always work and be
249
+ // safe. But such a cast is *not* safe on the uncommon platforms where
250
+ // `mem::sizeof(libc::size_t) < mem::size_t(usize)`. Consequently, to
251
+ // reduce the chances of programmers becoming habituated to such casts that
252
+ // would be unsafe on unusual platforms, we have adopted the following
253
+ // convention:
254
+ //
255
+ // * On common platforms where
256
+ // `mem::sizeof(libc::size_t) == mem::sizeof(usize)`, `libc::size_t` must
257
+ // be a type alias of `usize`, and `libc::ssize_t` must be a type alias
258
+ // of `isize`.
259
+ //
260
+ // * On uncommon platforms where
261
+ // `mem::sizeof(libc::size_t) != mem::sizeof(usize)`, `libc::size_t` and
262
+ // `libc::ssize_t` must be defined as types other than `usize` and
263
+ // `isize`.
264
+ //
265
+ // * Code that was written without consideration for the uncommon platforms
266
+ // should not do any explicit casting between `libc::size_t` and `usize`
267
+ // or `libc::ssize_t` and `isize`. Such code will fail to compile on the
268
+ // uncommon platforms; this is better than executing with unsafe
269
+ // truncations.
270
+ //
271
+ // * Code that was written with full consideration of the uncommon
272
+ // platforms should have explicit casts using `num::cast` or other
273
+ // methods that avoid unintended truncation. Such code will then work on
274
+ // all platforms.
231
275
232
276
#[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "nacl" ) ) ]
233
277
pub mod os {
@@ -429,7 +473,7 @@ pub mod types {
429
473
pub type c_ulong = u32 ;
430
474
pub type c_float = f32 ;
431
475
pub type c_double = f64 ;
432
- pub type size_t = u32 ;
476
+ pub type size_t = usize ;
433
477
pub type ptrdiff_t = i32 ;
434
478
pub type clock_t = i32 ;
435
479
pub type time_t = i32 ;
@@ -459,7 +503,7 @@ pub mod types {
459
503
pub type gid_t = u32 ;
460
504
pub type useconds_t = u32 ;
461
505
pub type mode_t = u32 ;
462
- pub type ssize_t = i32 ;
506
+ pub type ssize_t = isize ;
463
507
}
464
508
#[ cfg( all( any( target_arch = "arm" , target_arch = "x86" ) ,
465
509
target_os = "android" ) ) ]
@@ -474,7 +518,7 @@ pub mod types {
474
518
pub type useconds_t = u32 ;
475
519
476
520
pub type mode_t = u16 ;
477
- pub type ssize_t = i32 ;
521
+ pub type ssize_t = isize ;
478
522
}
479
523
#[ cfg( any( all( any( target_arch = "arm" , target_arch = "x86" ) ,
480
524
not( target_os = "android" ) ) ,
@@ -655,7 +699,7 @@ pub mod types {
655
699
pub type c_ulong = u64 ;
656
700
pub type c_float = f32 ;
657
701
pub type c_double = f64 ;
658
- pub type size_t = u64 ;
702
+ pub type size_t = usize ;
659
703
pub type ptrdiff_t = i64 ;
660
704
pub type clock_t = i64 ;
661
705
pub type time_t = i64 ;
@@ -682,7 +726,7 @@ pub mod types {
682
726
pub type gid_t = u32 ;
683
727
pub type useconds_t = u32 ;
684
728
pub type mode_t = u32 ;
685
- pub type ssize_t = i64 ;
729
+ pub type ssize_t = isize ;
686
730
}
687
731
#[ cfg( not( target_arch = "aarch64" ) ) ]
688
732
pub mod posix01 {
@@ -980,7 +1024,7 @@ pub mod types {
980
1024
pub type c_ulong = u32 ;
981
1025
pub type c_float = f32 ;
982
1026
pub type c_double = f64 ;
983
- pub type size_t = u32 ;
1027
+ pub type size_t = usize ;
984
1028
pub type ptrdiff_t = i32 ;
985
1029
pub type clock_t = i32 ;
986
1030
pub type time_t = i32 ;
@@ -1004,7 +1048,7 @@ pub mod types {
1004
1048
pub type gid_t = u32 ;
1005
1049
pub type useconds_t = u32 ;
1006
1050
pub type mode_t = u16 ;
1007
- pub type ssize_t = i32 ;
1051
+ pub type ssize_t = isize ;
1008
1052
}
1009
1053
pub mod posix01 {
1010
1054
use types:: common:: c95:: { c_void} ;
@@ -1074,7 +1118,7 @@ pub mod types {
1074
1118
pub type c_ulong = u64 ;
1075
1119
pub type c_float = f32 ;
1076
1120
pub type c_double = f64 ;
1077
- pub type size_t = u64 ;
1121
+ pub type size_t = usize ;
1078
1122
pub type ptrdiff_t = i64 ;
1079
1123
pub type clock_t = i32 ;
1080
1124
pub type time_t = i64 ;
@@ -1098,7 +1142,7 @@ pub mod types {
1098
1142
pub type gid_t = u32 ;
1099
1143
pub type useconds_t = u32 ;
1100
1144
pub type mode_t = u16 ;
1101
- pub type ssize_t = i64 ;
1145
+ pub type ssize_t = isize ;
1102
1146
}
1103
1147
pub mod posix01 {
1104
1148
use types:: common:: c95:: { c_void} ;
@@ -1340,7 +1384,7 @@ pub mod types {
1340
1384
pub type c_ulong = u64 ;
1341
1385
pub type c_float = f32 ;
1342
1386
pub type c_double = f64 ;
1343
- pub type size_t = u64 ;
1387
+ pub type size_t = usize ;
1344
1388
pub type ptrdiff_t = i64 ;
1345
1389
pub type clock_t = i32 ;
1346
1390
pub type time_t = i64 ;
@@ -1363,7 +1407,7 @@ pub mod types {
1363
1407
pub type gid_t = u32 ;
1364
1408
pub type useconds_t = u32 ;
1365
1409
pub type mode_t = u16 ;
1366
- pub type ssize_t = i64 ;
1410
+ pub type ssize_t = isize ;
1367
1411
}
1368
1412
pub mod posix01 {
1369
1413
use types:: common:: c95:: { c_void} ;
@@ -1625,7 +1669,7 @@ pub mod types {
1625
1669
pub type c_ulong = u64 ;
1626
1670
pub type c_float = f32 ;
1627
1671
pub type c_double = f64 ;
1628
- pub type size_t = u64 ;
1672
+ pub type size_t = usize ;
1629
1673
pub type ptrdiff_t = i64 ;
1630
1674
pub type clock_t = i64 ;
1631
1675
pub type time_t = i64 ;
@@ -1649,7 +1693,7 @@ pub mod types {
1649
1693
pub type gid_t = u32 ;
1650
1694
pub type useconds_t = u32 ;
1651
1695
pub type mode_t = u32 ;
1652
- pub type ssize_t = c_long ;
1696
+ pub type ssize_t = isize ;
1653
1697
}
1654
1698
pub mod posix01 {
1655
1699
use types:: common:: c95:: { c_void} ;
@@ -1845,10 +1889,7 @@ pub mod types {
1845
1889
pub type c_float = f32 ;
1846
1890
pub type c_double = f64 ;
1847
1891
1848
- #[ cfg( target_arch = "x86" ) ]
1849
- pub type size_t = u32 ;
1850
- #[ cfg( target_arch = "x86_64" ) ]
1851
- pub type size_t = u64 ;
1892
+ pub type size_t = usize ;
1852
1893
1853
1894
#[ cfg( target_arch = "x86" ) ]
1854
1895
pub type ptrdiff_t = i32 ;
@@ -1898,10 +1939,7 @@ pub mod types {
1898
1939
pub type useconds_t = u32 ;
1899
1940
pub type mode_t = u16 ;
1900
1941
1901
- #[ cfg( target_arch = "x86" ) ]
1902
- pub type ssize_t = i32 ;
1903
- #[ cfg( target_arch = "x86_64" ) ]
1904
- pub type ssize_t = i64 ;
1942
+ pub type ssize_t = isize ;
1905
1943
}
1906
1944
1907
1945
pub mod posix01 {
@@ -2345,7 +2383,7 @@ pub mod types {
2345
2383
pub type gid_t = u32 ;
2346
2384
pub type useconds_t = u32 ;
2347
2385
pub type mode_t = u16 ;
2348
- pub type ssize_t = c_long ;
2386
+ pub type ssize_t = isize ;
2349
2387
}
2350
2388
pub mod posix01 {
2351
2389
use types:: common:: c99:: { int32_t, int64_t, uint32_t} ;
@@ -2453,7 +2491,7 @@ pub mod types {
2453
2491
pub type gid_t = u32 ;
2454
2492
pub type useconds_t = u32 ;
2455
2493
pub type mode_t = u16 ;
2456
- pub type ssize_t = c_long ;
2494
+ pub type ssize_t = isize ;
2457
2495
}
2458
2496
pub mod posix01 {
2459
2497
use types:: common:: c99:: { int32_t, int64_t} ;
0 commit comments