@@ -253,6 +253,50 @@ pub mod types {
253
253
}
254
254
255
255
// Standard types that are scalar but vary by OS and arch.
256
+ //
257
+ // ISO C's `size_t` is defined to be the type of the result of the
258
+ // `sizeof` operator and the type of the size parameter to `malloc`. That
259
+ // is, C's `size_t` is only required to hold the size of the largest object
260
+ // that can be allocated. In particular, it is legal for a C implementation
261
+ // to have a maximum object size smaller than the entire address space. For
262
+ // example, a C implementation may have an maximum object size of 2^32
263
+ // bytes with a 64-bit address space, and typedef `size_t` as `uint32_t` so
264
+ // that `sizeof(size_t) == 4` and `sizeof(void*) == 8`.
265
+ //
266
+ // Rust's `usize`, on the other hand, is defined to always be the same size
267
+ // as a pointer. This means that it is possible, in theory, to have a
268
+ // platform where `usize` can represent values that `size_t` cannot
269
+ // represent. However, on the vast majority of systems, `usize` and
270
+ // `size_t` are represented the same way. If it were required to explicitly
271
+ // cast `usize` to `size_t` on common platforms, then many programmers
272
+ // would habitually write expressions such as
273
+ // `my_slice.len() as libc::size_t` expecting this to always work and be
274
+ // safe. But such a cast is *not* safe on the uncommon platforms where
275
+ // `mem::sizeof(libc::size_t) < mem::size_t(usize)`. Consequently, to
276
+ // reduce the chances of programmers becoming habituated to such casts that
277
+ // would be unsafe on unusual platforms, we have adopted the following
278
+ // convention:
279
+ //
280
+ // * On common platforms where
281
+ // `mem::sizeof(libc::size_t) == mem::sizeof(usize)`, `libc::size_t` must
282
+ // be a type alias of `usize`, and `libc::ssize_t` must be a type alias
283
+ // of `isize`.
284
+ //
285
+ // * On uncommon platforms where
286
+ // `mem::sizeof(libc::size_t) != mem::sizeof(usize)`, `libc::size_t` and
287
+ // `libc::ssize_t` must be defined as types other than `usize` and
288
+ // `isize`.
289
+ //
290
+ // * Code that was written without consideration for the uncommon platforms
291
+ // should not do any explicit casting between `libc::size_t` and `usize`
292
+ // or `libc::ssize_t` and `isize`. Such code will fail to compile on the
293
+ // uncommon platforms; this is better than executing with unsafe
294
+ // truncations.
295
+ //
296
+ // * Code that was written with full consideration of the uncommon
297
+ // platforms should have explicit casts using `num::cast` or other
298
+ // methods that avoid unintended truncation. Such code will then work on
299
+ // all platforms.
256
300
257
301
#[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "nacl" ) ) ]
258
302
pub mod os {
@@ -471,7 +515,7 @@ pub mod types {
471
515
pub type c_ulong = u32 ;
472
516
pub type c_float = f32 ;
473
517
pub type c_double = f64 ;
474
- pub type size_t = u32 ;
518
+ pub type size_t = usize ;
475
519
pub type ptrdiff_t = i32 ;
476
520
pub type clock_t = i32 ;
477
521
pub type time_t = i32 ;
@@ -501,7 +545,7 @@ pub mod types {
501
545
pub type gid_t = u32 ;
502
546
pub type useconds_t = u32 ;
503
547
pub type mode_t = u32 ;
504
- pub type ssize_t = i32 ;
548
+ pub type ssize_t = isize ;
505
549
}
506
550
#[ cfg( all( any( target_arch = "arm" , target_arch = "x86" ) ,
507
551
target_os = "android" ) ) ]
@@ -516,7 +560,7 @@ pub mod types {
516
560
pub type useconds_t = u32 ;
517
561
518
562
pub type mode_t = u16 ;
519
- pub type ssize_t = i32 ;
563
+ pub type ssize_t = isize ;
520
564
}
521
565
#[ cfg( any( all( any( target_arch = "arm" , target_arch = "x86" ) ,
522
566
not( target_os = "android" ) ) ,
@@ -709,7 +753,7 @@ pub mod types {
709
753
pub type c_ulong = u64 ;
710
754
pub type c_float = f32 ;
711
755
pub type c_double = f64 ;
712
- pub type size_t = u64 ;
756
+ pub type size_t = usize ;
713
757
pub type ptrdiff_t = i64 ;
714
758
pub type clock_t = i64 ;
715
759
pub type time_t = i64 ;
@@ -736,7 +780,7 @@ pub mod types {
736
780
pub type gid_t = u32 ;
737
781
pub type useconds_t = u32 ;
738
782
pub type mode_t = u32 ;
739
- pub type ssize_t = i64 ;
783
+ pub type ssize_t = isize ;
740
784
}
741
785
#[ cfg( not( target_arch = "aarch64" ) ) ]
742
786
pub mod posix01 {
@@ -1058,7 +1102,7 @@ pub mod types {
1058
1102
pub type c_ulong = u32 ;
1059
1103
pub type c_float = f32 ;
1060
1104
pub type c_double = f64 ;
1061
- pub type size_t = u32 ;
1105
+ pub type size_t = usize ;
1062
1106
pub type ptrdiff_t = i32 ;
1063
1107
pub type clock_t = i32 ;
1064
1108
pub type time_t = i32 ;
@@ -1082,7 +1126,7 @@ pub mod types {
1082
1126
pub type gid_t = u32 ;
1083
1127
pub type useconds_t = u32 ;
1084
1128
pub type mode_t = u16 ;
1085
- pub type ssize_t = i32 ;
1129
+ pub type ssize_t = isize ;
1086
1130
}
1087
1131
pub mod posix01 {
1088
1132
use types:: common:: c95:: c_void;
@@ -1154,7 +1198,7 @@ pub mod types {
1154
1198
pub type c_ulong = u64 ;
1155
1199
pub type c_float = f32 ;
1156
1200
pub type c_double = f64 ;
1157
- pub type size_t = u64 ;
1201
+ pub type size_t = usize ;
1158
1202
pub type ptrdiff_t = i64 ;
1159
1203
pub type clock_t = i32 ;
1160
1204
pub type time_t = i64 ;
@@ -1178,7 +1222,7 @@ pub mod types {
1178
1222
pub type gid_t = u32 ;
1179
1223
pub type useconds_t = u32 ;
1180
1224
pub type mode_t = u16 ;
1181
- pub type ssize_t = i64 ;
1225
+ pub type ssize_t = isize ;
1182
1226
}
1183
1227
pub mod posix01 {
1184
1228
use types:: common:: c95:: c_void;
@@ -1439,7 +1483,7 @@ pub mod types {
1439
1483
pub type c_ulong = u64 ;
1440
1484
pub type c_float = f32 ;
1441
1485
pub type c_double = f64 ;
1442
- pub type size_t = u64 ;
1486
+ pub type size_t = usize ;
1443
1487
pub type ptrdiff_t = i64 ;
1444
1488
pub type clock_t = i32 ;
1445
1489
pub type time_t = i64 ;
@@ -1462,7 +1506,7 @@ pub mod types {
1462
1506
pub type gid_t = u32 ;
1463
1507
pub type useconds_t = u32 ;
1464
1508
pub type mode_t = u16 ;
1465
- pub type ssize_t = i64 ;
1509
+ pub type ssize_t = isize ;
1466
1510
}
1467
1511
pub mod posix01 {
1468
1512
use types:: common:: c95:: c_void;
@@ -1775,7 +1819,7 @@ pub mod types {
1775
1819
pub type c_ulong = u64 ;
1776
1820
pub type c_float = f32 ;
1777
1821
pub type c_double = f64 ;
1778
- pub type size_t = u64 ;
1822
+ pub type size_t = usize ;
1779
1823
pub type ptrdiff_t = i64 ;
1780
1824
pub type clock_t = i64 ;
1781
1825
pub type time_t = i64 ;
@@ -1799,7 +1843,7 @@ pub mod types {
1799
1843
pub type gid_t = u32 ;
1800
1844
pub type useconds_t = u32 ;
1801
1845
pub type mode_t = u32 ;
1802
- pub type ssize_t = c_long ;
1846
+ pub type ssize_t = isize ;
1803
1847
}
1804
1848
pub mod posix01 {
1805
1849
use types:: common:: c95:: c_void;
@@ -2054,10 +2098,7 @@ pub mod types {
2054
2098
pub type c_float = f32 ;
2055
2099
pub type c_double = f64 ;
2056
2100
2057
- #[ cfg( target_arch = "x86" ) ]
2058
- pub type size_t = u32 ;
2059
- #[ cfg( target_arch = "x86_64" ) ]
2060
- pub type size_t = u64 ;
2101
+ pub type size_t = usize ;
2061
2102
2062
2103
#[ cfg( target_arch = "x86" ) ]
2063
2104
pub type ptrdiff_t = i32 ;
@@ -2107,10 +2148,7 @@ pub mod types {
2107
2148
pub type useconds_t = u32 ;
2108
2149
pub type mode_t = u16 ;
2109
2150
2110
- #[ cfg( target_arch = "x86" ) ]
2111
- pub type ssize_t = i32 ;
2112
- #[ cfg( target_arch = "x86_64" ) ]
2113
- pub type ssize_t = i64 ;
2151
+ pub type ssize_t = isize ;
2114
2152
}
2115
2153
2116
2154
pub mod posix01 {
@@ -2586,7 +2624,7 @@ pub mod types {
2586
2624
pub type gid_t = u32 ;
2587
2625
pub type useconds_t = u32 ;
2588
2626
pub type mode_t = u16 ;
2589
- pub type ssize_t = c_long ;
2627
+ pub type ssize_t = isize ;
2590
2628
}
2591
2629
pub mod posix01 {
2592
2630
use types:: common:: c99:: { int32_t, int64_t, uint32_t} ;
@@ -2699,7 +2737,7 @@ pub mod types {
2699
2737
pub type gid_t = u32 ;
2700
2738
pub type useconds_t = u32 ;
2701
2739
pub type mode_t = u16 ;
2702
- pub type ssize_t = c_long ;
2740
+ pub type ssize_t = isize ;
2703
2741
}
2704
2742
pub mod posix01 {
2705
2743
use types:: common:: c99:: { int32_t, int64_t} ;
0 commit comments