| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.typecons; |
| 4 |
|
| 5 |
alias Coord = Tuple!(int, "x", int, "y", int, "z"); |
| 6 |
Coord c; |
| 7 |
c[1] = 1; // access by index |
| 8 |
c.z = 1; // access by given name |
| 9 |
assert(c == Coord(0, 1, 1)); |
| 10 |
|
| 11 |
// names can be omitted, types can be mixed |
| 12 |
alias DictEntry = Tuple!(string, int); |
| 13 |
auto dict = DictEntry("seven", 7); |
| 14 |
|
| 15 |
// element types can be inferred |
| 16 |
assert(tuple(2, 3, 4)[1] == 3); |
| 17 |
// type inference works with names too |
| 18 |
auto tup = tuple!("x", "y", "z")(2, 3, 4); |
| 19 |
assert(tup.y == 3); |
| 20 |
} |
| 21 |
|
| 22 |
@safe unittest |
| 23 |
{ |
| 24 |
import std.typecons; |
| 25 |
|
| 26 |
class Widget |
| 27 |
{ |
| 28 |
void foo() const @safe {} |
| 29 |
} |
| 30 |
const w1 = new Widget, w2 = new Widget; |
| 31 |
w1.foo(); |
| 32 |
// w1 = w2 would not work; can't rebind const object |
| 33 |
|
| 34 |
auto r = Rebindable!(const Widget)(w1); |
| 35 |
// invoke method as if r were a Widget object |
| 36 |
r.foo(); |
| 37 |
// rebind r to refer to another object |
| 38 |
r = w2; |
| 39 |
} |
| 40 |
|
| 41 |
@safe unittest |
| 42 |
{ |
| 43 |
import std.typecons; |
| 44 |
|
| 45 |
struct S |
| 46 |
{ |
| 47 |
int i; |
| 48 |
this(int i){this.i = i;} |
| 49 |
} |
| 50 |
Unique!S produce() |
| 51 |
{ |
| 52 |
// Construct a unique instance of S on the heap |
| 53 |
Unique!S ut = new S(5); |
| 54 |
// Implicit transfer of ownership |
| 55 |
return ut; |
| 56 |
} |
| 57 |
// Borrow a unique resource by ref |
| 58 |
void increment(ref Unique!S ur) |
| 59 |
{ |
| 60 |
ur.i++; |
| 61 |
} |
| 62 |
void consume(Unique!S u2) |
| 63 |
{ |
| 64 |
assert(u2.i == 6); |
| 65 |
// Resource automatically deleted here |
| 66 |
} |
| 67 |
Unique!S u1; |
| 68 |
assert(u1.isEmpty); |
| 69 |
u1 = produce(); |
| 70 |
assert(u1.i == 5); |
| 71 |
increment(u1); |
| 72 |
assert(u1.i == 6); |
| 73 |
//consume(u1); // Error: u1 is not copyable |
| 74 |
// Transfer ownership of the resource |
| 75 |
consume(u1.release); |
| 76 |
assert(u1.isEmpty); |
| 77 |
} |
| 78 |
|
| 79 |
@safe unittest |
| 80 |
{ |
| 81 |
import std.typecons; |
| 82 |
|
| 83 |
import std.meta : AliasSeq; |
| 84 |
alias Fields = Tuple!(int, "id", string, float); |
| 85 |
static assert(is(Fields.Types == AliasSeq!(int, string, float))); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
@safe unittest |
| 90 |
{ |
| 91 |
import std.typecons; |
| 92 |
|
| 93 |
import std.meta : AliasSeq; |
| 94 |
alias Fields = Tuple!(int, "id", string, float); |
| 95 |
static assert(Fields.fieldNames == AliasSeq!("id", "", "")); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
@safe unittest |
| 100 |
{ |
| 101 |
import std.typecons; |
| 102 |
|
| 103 |
auto t1 = tuple(1, " hello ", 'a'); |
| 104 |
assert(t1.toString() == `Tuple!(int, string, char)(1, " hello ", 'a')`); |
| 105 |
|
| 106 |
void takeSeveralTypes(int n, string s, bool b) |
| 107 |
{ |
| 108 |
assert(n == 4 && s == "test" && b == false); |
| 109 |
} |
| 110 |
|
| 111 |
auto t2 = tuple(4, "test", false); |
| 112 |
//t.expand acting as a list of values |
| 113 |
takeSeveralTypes(t2.expand); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
@safe unittest |
| 118 |
{ |
| 119 |
import std.typecons; |
| 120 |
|
| 121 |
alias ISD = Tuple!(int, string, double); |
| 122 |
auto tup = ISD(1, "test", 3.2); |
| 123 |
assert(tup.toString() == `Tuple!(int, string, double)(1, "test", 3.2)`); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
@safe unittest |
| 128 |
{ |
| 129 |
import std.typecons; |
| 130 |
|
| 131 |
int[2] ints; |
| 132 |
Tuple!(int, int) t = ints; |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
@safe unittest |
| 137 |
{ |
| 138 |
import std.typecons; |
| 139 |
|
| 140 |
alias IntVec = Tuple!(int, int, int); |
| 141 |
alias DubVec = Tuple!(double, double, double); |
| 142 |
|
| 143 |
IntVec iv = tuple(1, 1, 1); |
| 144 |
|
| 145 |
//Ok, int can implicitly convert to double |
| 146 |
DubVec dv = iv; |
| 147 |
//Error: double cannot implicitly convert to int |
| 148 |
//IntVec iv2 = dv; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
@safe unittest |
| 153 |
{ |
| 154 |
import std.typecons; |
| 155 |
|
| 156 |
Tuple!(int, string) t1 = tuple(1, "test"); |
| 157 |
Tuple!(double, string) t2 = tuple(1.0, "test"); |
| 158 |
//Ok, int can be compared with double and |
| 159 |
//both have a value of 1 |
| 160 |
assert(t1 == t2); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
@safe unittest |
| 165 |
{ |
| 166 |
import std.typecons; |
| 167 |
|
| 168 |
auto tup1 = tuple(1, 1, 1); |
| 169 |
auto tup2 = tuple(1, 100, 100); |
| 170 |
assert(tup1 < tup2); |
| 171 |
|
| 172 |
//Only the first result matters for comparison |
| 173 |
tup1[0] = 2; |
| 174 |
assert(tup1 > tup2); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
@safe unittest |
| 179 |
{ |
| 180 |
import std.typecons; |
| 181 |
|
| 182 |
auto t0 = tuple(4, "hello"); |
| 183 |
|
| 184 |
auto t0Named = t0.rename!("val", "tag"); |
| 185 |
assert(t0Named.val == 4); |
| 186 |
assert(t0Named.tag == "hello"); |
| 187 |
|
| 188 |
Tuple!(float, "dat", size_t[2], "pos") t1; |
| 189 |
t1.pos = [2, 1]; |
| 190 |
auto t1Named = t1.rename!"height"; |
| 191 |
t1Named.height = 3.4f; |
| 192 |
assert(t1Named.height == 3.4f); |
| 193 |
assert(t1Named.pos == [2, 1]); |
| 194 |
t1Named.rename!"altitude".altitude = 5; |
| 195 |
assert(t1Named.height == 5); |
| 196 |
|
| 197 |
Tuple!(int, "a", int, int, "c") t2; |
| 198 |
t2 = tuple(3,4,5); |
| 199 |
auto t2Named = t2.rename!("", "b"); |
| 200 |
// "a" no longer has a name |
| 201 |
static assert(!__traits(hasMember, typeof(t2Named), "a")); |
| 202 |
assert(t2Named[0] == 3); |
| 203 |
assert(t2Named.b == 4); |
| 204 |
assert(t2Named.c == 5); |
| 205 |
|
| 206 |
// not allowed to specify more names than the tuple has members |
| 207 |
static assert(!__traits(compiles, t2.rename!("a","b","c","d"))); |
| 208 |
|
| 209 |
// use it in a range pipeline |
| 210 |
import std.range : iota, zip; |
| 211 |
import std.algorithm.iteration : map, sum; |
| 212 |
auto res = zip(iota(1, 4), iota(10, 13)) |
| 213 |
.map!(t => t.rename!("a", "b")) |
| 214 |
.map!(t => t.a * t.b) |
| 215 |
.sum; |
| 216 |
assert(res == 68); |
| 217 |
|
| 218 |
const tup = Tuple!(int, "a", int, "b")(2, 3); |
| 219 |
const renamed = tup.rename!("c", "d"); |
| 220 |
assert(renamed.c + renamed.d == 5); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
@safe unittest |
| 225 |
{ |
| 226 |
import std.typecons; |
| 227 |
|
| 228 |
//replacing names by their current name |
| 229 |
|
| 230 |
Tuple!(float, "dat", size_t[2], "pos") t1; |
| 231 |
t1.pos = [2, 1]; |
| 232 |
auto t1Named = t1.rename!(["dat": "height"]); |
| 233 |
t1Named.height = 3.4; |
| 234 |
assert(t1Named.pos == [2, 1]); |
| 235 |
t1Named.rename!(["height": "altitude"]).altitude = 5; |
| 236 |
assert(t1Named.height == 5); |
| 237 |
|
| 238 |
Tuple!(int, "a", int, "b") t2; |
| 239 |
t2 = tuple(3, 4); |
| 240 |
auto t2Named = t2.rename!(["a": "b", "b": "c"]); |
| 241 |
assert(t2Named.b == 3); |
| 242 |
assert(t2Named.c == 4); |
| 243 |
|
| 244 |
const t3 = Tuple!(int, "a", int, "b")(3, 4); |
| 245 |
const t3Named = t3.rename!(["a": "b", "b": "c"]); |
| 246 |
assert(t3Named.b == 3); |
| 247 |
assert(t3Named.c == 4); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
@system unittest |
| 252 |
{ |
| 253 |
import std.typecons; |
| 254 |
|
| 255 |
//replace names by their position |
| 256 |
|
| 257 |
Tuple!(float, "dat", size_t[2], "pos") t1; |
| 258 |
t1.pos = [2, 1]; |
| 259 |
auto t1Named = t1.rename!([0: "height"]); |
| 260 |
t1Named.height = 3.4; |
| 261 |
assert(t1Named.pos == [2, 1]); |
| 262 |
t1Named.rename!([0: "altitude"]).altitude = 5; |
| 263 |
assert(t1Named.height == 5); |
| 264 |
|
| 265 |
Tuple!(int, "a", int, "b", int, "c") t2; |
| 266 |
t2 = tuple(3, 4, 5); |
| 267 |
auto t2Named = t2.rename!([0: "c", 2: "a"]); |
| 268 |
assert(t2Named.a == 5); |
| 269 |
assert(t2Named.b == 4); |
| 270 |
assert(t2Named.c == 3); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
@safe unittest |
| 275 |
{ |
| 276 |
import std.typecons; |
| 277 |
|
| 278 |
Tuple!(int, string, float, double) a; |
| 279 |
a[1] = "abc"; |
| 280 |
a[2] = 4.5; |
| 281 |
auto s = a.slice!(1, 3); |
| 282 |
static assert(is(typeof(s) == Tuple!(string, float))); |
| 283 |
assert(s[0] == "abc" && s[1] == 4.5); |
| 284 |
|
| 285 |
// https://issues.dlang.org/show_bug.cgi?id=15645 |
| 286 |
Tuple!(int, short, bool, double) b; |
| 287 |
static assert(!__traits(compiles, b.slice!(2, 4))); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
@safe unittest |
| 292 |
{ |
| 293 |
import std.typecons; |
| 294 |
|
| 295 |
import std.format : format; |
| 296 |
|
| 297 |
Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ]; |
| 298 |
|
| 299 |
// Default format |
| 300 |
assert(format("%s", tuple("a", 1)) == `Tuple!(string, int)("a", 1)`); |
| 301 |
|
| 302 |
// One Format for each individual component |
| 303 |
assert(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10)) == `0x1 v 1.0000 w 0xa`); |
| 304 |
assert(format( "%#x v %.4f w %#x" , tuple(1, 1.0, 10).expand) == `0x1 v 1.0000 w 0xa`); |
| 305 |
|
| 306 |
// One Format for all components |
| 307 |
assert(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])) == `>abc< & >1< & >2.3< & >[4, 5]<`); |
| 308 |
|
| 309 |
// Array of Tuples |
| 310 |
assert(format("%(%(f(%d) = %.1f%); %)", tupList) == `f(1) = 1.0; f(2) = 4.0; f(3) = 9.0`); |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
@safe unittest |
| 315 |
{ |
| 316 |
import std.typecons; |
| 317 |
|
| 318 |
import std.exception : assertThrown; |
| 319 |
import std.format : format, FormatException; |
| 320 |
|
| 321 |
// Error: %( %) missing. |
| 322 |
assertThrown!FormatException( |
| 323 |
format("%d, %f", tuple(1, 2.0)) == `1, 2.0` |
| 324 |
); |
| 325 |
|
| 326 |
// Error: %( %| %) missing. |
| 327 |
assertThrown!FormatException( |
| 328 |
format("%d", tuple(1, 2)) == `1, 2` |
| 329 |
); |
| 330 |
|
| 331 |
// Error: %d inadequate for double |
| 332 |
assertThrown!FormatException( |
| 333 |
format("%(%d%|, %)", tuple(1, 2.0)) == `1, 2.0` |
| 334 |
); |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
@safe unittest |
| 339 |
{ |
| 340 |
import std.typecons; |
| 341 |
|
| 342 |
Tuple!(int, int) point; |
| 343 |
// assign coordinates |
| 344 |
point[0] = 5; |
| 345 |
point[1] = 6; |
| 346 |
// read coordinates |
| 347 |
auto x = point[0]; |
| 348 |
auto y = point[1]; |
| 349 |
} |
| 350 |
|
| 351 |
@safe unittest |
| 352 |
{ |
| 353 |
import std.typecons; |
| 354 |
|
| 355 |
alias Entry = Tuple!(int, "index", string, "value"); |
| 356 |
Entry e; |
| 357 |
e.index = 4; |
| 358 |
e.value = "Hello"; |
| 359 |
assert(e[1] == "Hello"); |
| 360 |
assert(e[0] == 4); |
| 361 |
} |
| 362 |
|
| 363 |
@safe unittest |
| 364 |
{ |
| 365 |
import std.typecons; |
| 366 |
|
| 367 |
Tuple!(int, "x", int, "y") point1; |
| 368 |
Tuple!(int, int) point2; |
| 369 |
assert(!is(typeof(point1) == typeof(point2))); |
| 370 |
} |
| 371 |
|
| 372 |
@safe unittest |
| 373 |
{ |
| 374 |
import std.typecons; |
| 375 |
|
| 376 |
import std.algorithm.iteration : sum; |
| 377 |
import std.range : only; |
| 378 |
auto t = tuple(1, 2); |
| 379 |
assert(t.expand.only.sum == 3); |
| 380 |
} |
| 381 |
|
| 382 |
@safe unittest |
| 383 |
{ |
| 384 |
import std.typecons; |
| 385 |
|
| 386 |
import std.meta : AliasSeq; |
| 387 |
auto t = tuple(1, "2") ~ tuple(ushort(42), true); |
| 388 |
static assert(is(t.Types == AliasSeq!(int, string, ushort, bool))); |
| 389 |
assert(t[1] == "2"); |
| 390 |
assert(t[2] == 42); |
| 391 |
assert(t[3] == true); |
| 392 |
} |
| 393 |
|
| 394 |
@safe unittest |
| 395 |
{ |
| 396 |
import std.typecons; |
| 397 |
|
| 398 |
auto tup = tuple(1, "2"); |
| 399 |
assert(tup.reverse == tuple("2", 1)); |
| 400 |
} |
| 401 |
|
| 402 |
@safe unittest |
| 403 |
{ |
| 404 |
import std.typecons; |
| 405 |
|
| 406 |
auto value = tuple(5, 6.7, "hello"); |
| 407 |
assert(value[0] == 5); |
| 408 |
assert(value[1] == 6.7); |
| 409 |
assert(value[2] == "hello"); |
| 410 |
|
| 411 |
// Field names can be provided. |
| 412 |
auto entry = tuple!("index", "value")(4, "Hello"); |
| 413 |
assert(entry.index == 4); |
| 414 |
assert(entry.value == "Hello"); |
| 415 |
} |
| 416 |
|
| 417 |
@safe unittest |
| 418 |
{ |
| 419 |
import std.typecons; |
| 420 |
|
| 421 |
static assert(isTuple!(Tuple!())); |
| 422 |
static assert(isTuple!(Tuple!(int))); |
| 423 |
static assert(isTuple!(Tuple!(int, real, string))); |
| 424 |
static assert(isTuple!(Tuple!(int, "x", real, "y"))); |
| 425 |
static assert(isTuple!(Tuple!(int, Tuple!(real), string))); |
| 426 |
} |
| 427 |
|
| 428 |
@safe unittest |
| 429 |
{ |
| 430 |
import std.typecons; |
| 431 |
|
| 432 |
class Widget { int x; int y() @safe const { return x; } } |
| 433 |
const a = new Widget; |
| 434 |
// Fine |
| 435 |
a.y(); |
| 436 |
// error! can't modify const a |
| 437 |
// a.x = 5; |
| 438 |
// error! can't modify const a |
| 439 |
// a = new Widget; |
| 440 |
} |
| 441 |
|
| 442 |
@safe unittest |
| 443 |
{ |
| 444 |
import std.typecons; |
| 445 |
|
| 446 |
class Widget { int x; int y() const @safe { return x; } } |
| 447 |
auto a = Rebindable!(const Widget)(new Widget); |
| 448 |
// Fine |
| 449 |
a.y(); |
| 450 |
// error! can't modify const a |
| 451 |
// a.x = 5; |
| 452 |
// Fine |
| 453 |
a = new Widget; |
| 454 |
} |
| 455 |
|
| 456 |
@safe unittest |
| 457 |
{ |
| 458 |
import std.typecons; |
| 459 |
|
| 460 |
import std.range.primitives : front, popFront; |
| 461 |
|
| 462 |
// simple version of std.algorithm.searching.maxElement |
| 463 |
typeof(R.init.front) maxElement(R)(R r) |
| 464 |
{ |
| 465 |
auto max = rebindable(r.front); |
| 466 |
r.popFront; |
| 467 |
foreach (e; r) |
| 468 |
if (e > max) |
| 469 |
max = e; // Rebindable allows const-correct reassignment |
| 470 |
return max; |
| 471 |
} |
| 472 |
struct S |
| 473 |
{ |
| 474 |
char[] arr; |
| 475 |
alias arr this; // for comparison |
| 476 |
} |
| 477 |
// can't convert to mutable |
| 478 |
const S cs; |
| 479 |
static assert(!__traits(compiles, { S s = cs; })); |
| 480 |
|
| 481 |
alias CS = const S; |
| 482 |
CS[] arr = [CS("harp"), CS("apple"), CS("pot")]; |
| 483 |
CS ms = maxElement(arr); |
| 484 |
assert(ms.arr == "pot"); |
| 485 |
} |
| 486 |
|
| 487 |
@system unittest |
| 488 |
{ |
| 489 |
import std.typecons; |
| 490 |
|
| 491 |
static struct S |
| 492 |
{ |
| 493 |
int* ptr; |
| 494 |
} |
| 495 |
S s = S(new int); |
| 496 |
|
| 497 |
const cs = s; |
| 498 |
// Can't assign s.ptr to cs.ptr |
| 499 |
static assert(!__traits(compiles, {s = cs;})); |
| 500 |
|
| 501 |
Rebindable!(const S) rs = s; |
| 502 |
assert(rs.ptr is s.ptr); |
| 503 |
// rs.ptr is const |
| 504 |
static assert(!__traits(compiles, {rs.ptr = null;})); |
| 505 |
|
| 506 |
// Can't assign s.ptr to rs.ptr |
| 507 |
static assert(!__traits(compiles, {s = rs;})); |
| 508 |
|
| 509 |
const S cs2 = rs; |
| 510 |
// Rebind rs |
| 511 |
rs = cs2; |
| 512 |
rs = S(); |
| 513 |
assert(rs.ptr is null); |
| 514 |
} |
| 515 |
|
| 516 |
@system unittest |
| 517 |
{ |
| 518 |
import std.typecons; |
| 519 |
|
| 520 |
class C |
| 521 |
{ |
| 522 |
int payload; |
| 523 |
this(int p) { payload = p; } |
| 524 |
} |
| 525 |
const c = new C(1); |
| 526 |
|
| 527 |
auto c2 = c.rebindable; |
| 528 |
assert(c2.payload == 1); |
| 529 |
// passing Rebindable to rebindable |
| 530 |
c2 = c2.rebindable; |
| 531 |
|
| 532 |
c2 = new C(2); |
| 533 |
assert(c2.payload == 2); |
| 534 |
|
| 535 |
const c3 = c2.get; |
| 536 |
assert(c3.payload == 2); |
| 537 |
} |
| 538 |
|
| 539 |
@safe unittest |
| 540 |
{ |
| 541 |
import std.typecons; |
| 542 |
|
| 543 |
immutable struct S |
| 544 |
{ |
| 545 |
int[] array; |
| 546 |
} |
| 547 |
auto s1 = [3].idup.rebindable; |
| 548 |
s1 = [4].idup.rebindable; |
| 549 |
assert(s1 == [4]); |
| 550 |
} |
| 551 |
|
| 552 |
@system unittest |
| 553 |
{ |
| 554 |
import std.typecons; |
| 555 |
|
| 556 |
class C |
| 557 |
{ |
| 558 |
int payload; |
| 559 |
this(int p) { payload = p; } |
| 560 |
} |
| 561 |
const c = new C(1); |
| 562 |
|
| 563 |
auto c2 = c.rebindable; |
| 564 |
assert(c2.payload == 1); |
| 565 |
// passing Rebindable to rebindable |
| 566 |
c2 = c2.rebindable; |
| 567 |
assert(c2.payload == 1); |
| 568 |
} |
| 569 |
|
| 570 |
@system unittest |
| 571 |
{ |
| 572 |
import std.typecons; |
| 573 |
|
| 574 |
class Data {} |
| 575 |
|
| 576 |
static shared(Data) a; |
| 577 |
static UnqualRef!(shared Data) b; |
| 578 |
|
| 579 |
import core.thread; |
| 580 |
|
| 581 |
auto thread = new core.thread.Thread({ |
| 582 |
a = new shared Data(); |
| 583 |
b = new shared Data(); |
| 584 |
}); |
| 585 |
|
| 586 |
thread.start(); |
| 587 |
thread.join(); |
| 588 |
|
| 589 |
assert(a !is null); |
| 590 |
assert(b is null); |
| 591 |
} |
| 592 |
|
| 593 |
@safe unittest |
| 594 |
{ |
| 595 |
import std.typecons; |
| 596 |
|
| 597 |
struct Banner { |
| 598 |
mixin(alignForSize!(byte[6], double)(["name", "height"])); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
@safe unittest |
| 603 |
{ |
| 604 |
import std.typecons; |
| 605 |
|
| 606 |
Nullable!int empty; |
| 607 |
Nullable!int a = 42; |
| 608 |
Nullable!int b = 42; |
| 609 |
Nullable!int c = 27; |
| 610 |
|
| 611 |
assert(empty == empty); |
| 612 |
assert(empty == Nullable!int.init); |
| 613 |
assert(empty != a); |
| 614 |
assert(empty != b); |
| 615 |
assert(empty != c); |
| 616 |
|
| 617 |
assert(a == b); |
| 618 |
assert(a != c); |
| 619 |
|
| 620 |
assert(empty != 42); |
| 621 |
assert(a == 42); |
| 622 |
assert(c != 42); |
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
@safe unittest |
| 627 |
{ |
| 628 |
import std.typecons; |
| 629 |
|
| 630 |
Nullable!int ni; |
| 631 |
assert(ni.isNull); |
| 632 |
|
| 633 |
ni = 0; |
| 634 |
assert(!ni.isNull); |
| 635 |
|
| 636 |
} |
| 637 |
|
| 638 |
@safe unittest |
| 639 |
{ |
| 640 |
import std.typecons; |
| 641 |
|
| 642 |
Nullable!int ni = 0; |
| 643 |
assert(!ni.isNull); |
| 644 |
|
| 645 |
ni.nullify(); |
| 646 |
assert(ni.isNull); |
| 647 |
|
| 648 |
} |
| 649 |
|
| 650 |
@safe unittest |
| 651 |
{ |
| 652 |
import std.typecons; |
| 653 |
|
| 654 |
//Passes |
| 655 |
Nullable!(int*) npi; |
| 656 |
assert(npi.isNull); |
| 657 |
|
| 658 |
//Passes?! |
| 659 |
npi = null; |
| 660 |
assert(!npi.isNull); |
| 661 |
|
| 662 |
} |
| 663 |
|
| 664 |
@safe unittest |
| 665 |
{ |
| 666 |
import std.typecons; |
| 667 |
|
| 668 |
struct CustomerRecord |
| 669 |
{ |
| 670 |
string name; |
| 671 |
string address; |
| 672 |
int customerNum; |
| 673 |
} |
| 674 |
|
| 675 |
Nullable!CustomerRecord getByName(string name) |
| 676 |
{ |
| 677 |
//A bunch of hairy stuff |
| 678 |
|
| 679 |
return Nullable!CustomerRecord.init; |
| 680 |
} |
| 681 |
|
| 682 |
auto queryResult = getByName("Doe, John"); |
| 683 |
if (!queryResult.isNull) |
| 684 |
{ |
| 685 |
//Process Mr. Doe's customer record |
| 686 |
auto address = queryResult.get.address; |
| 687 |
auto customerNum = queryResult.get.customerNum; |
| 688 |
|
| 689 |
//Do some things with this customer's info |
| 690 |
} |
| 691 |
else |
| 692 |
{ |
| 693 |
//Add the customer to the database |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
@system unittest |
| 698 |
{ |
| 699 |
import std.typecons; |
| 700 |
|
| 701 |
import std.exception : assertThrown; |
| 702 |
|
| 703 |
auto a = 42.nullable; |
| 704 |
assert(!a.isNull); |
| 705 |
assert(a.get == 42); |
| 706 |
|
| 707 |
a.nullify(); |
| 708 |
assert(a.isNull); |
| 709 |
assertThrown!Throwable(a.get); |
| 710 |
} |
| 711 |
|
| 712 |
@safe unittest |
| 713 |
{ |
| 714 |
import std.typecons; |
| 715 |
|
| 716 |
import std.algorithm.iteration : each, joiner; |
| 717 |
Nullable!int a = 42; |
| 718 |
Nullable!int b; |
| 719 |
// Add each value to an array |
| 720 |
int[] arr; |
| 721 |
a.each!((n) => arr ~= n); |
| 722 |
assert(arr == [42]); |
| 723 |
b.each!((n) => arr ~= n); |
| 724 |
assert(arr == [42]); |
| 725 |
// Take first value from an array of Nullables |
| 726 |
Nullable!int[] c = new Nullable!int[](10); |
| 727 |
c[7] = Nullable!int(42); |
| 728 |
assert(c.joiner.front == 42); |
| 729 |
} |
| 730 |
|
| 731 |
@safe unittest |
| 732 |
{ |
| 733 |
import std.typecons; |
| 734 |
|
| 735 |
Nullable!(int, -1) ni; |
| 736 |
//Initialized to "null" state |
| 737 |
assert(ni.isNull); |
| 738 |
|
| 739 |
ni = 0; |
| 740 |
assert(!ni.isNull); |
| 741 |
} |
| 742 |
|
| 743 |
@safe unittest |
| 744 |
{ |
| 745 |
import std.typecons; |
| 746 |
|
| 747 |
Nullable!(int, -1) ni = 0; |
| 748 |
assert(!ni.isNull); |
| 749 |
|
| 750 |
ni = -1; |
| 751 |
assert(ni.isNull); |
| 752 |
} |
| 753 |
|
| 754 |
@system unittest |
| 755 |
{ |
| 756 |
import std.typecons; |
| 757 |
|
| 758 |
//Passes |
| 759 |
enum nullVal = cast(int*) 0xCAFEBABE; |
| 760 |
Nullable!(int*, nullVal) npi; |
| 761 |
assert(npi.isNull); |
| 762 |
|
| 763 |
//Passes?! |
| 764 |
npi = null; |
| 765 |
assert(!npi.isNull); |
| 766 |
} |
| 767 |
|
| 768 |
@system unittest |
| 769 |
{ |
| 770 |
import std.typecons; |
| 771 |
|
| 772 |
import std.exception : assertThrown, assertNotThrown; |
| 773 |
|
| 774 |
Nullable!(int, -1) ni; |
| 775 |
//`get` is implicitly called. Will throw |
| 776 |
//an error in non-release mode |
| 777 |
assertThrown!Throwable(ni == 0); |
| 778 |
|
| 779 |
ni = 0; |
| 780 |
assertNotThrown!Throwable(ni == 0); |
| 781 |
} |
| 782 |
|
| 783 |
@safe unittest |
| 784 |
{ |
| 785 |
import std.typecons; |
| 786 |
|
| 787 |
Nullable!(size_t, size_t.max) indexOf(string[] haystack, string needle) |
| 788 |
{ |
| 789 |
//Find the needle, returning -1 if not found |
| 790 |
|
| 791 |
return Nullable!(size_t, size_t.max).init; |
| 792 |
} |
| 793 |
|
| 794 |
void sendLunchInvite(string name) |
| 795 |
{ |
| 796 |
} |
| 797 |
|
| 798 |
//It's safer than C... |
| 799 |
auto coworkers = ["Jane", "Jim", "Marry", "Fred"]; |
| 800 |
auto pos = indexOf(coworkers, "Bob"); |
| 801 |
if (!pos.isNull) |
| 802 |
{ |
| 803 |
//Send Bob an invitation to lunch |
| 804 |
sendLunchInvite(coworkers[pos]); |
| 805 |
} |
| 806 |
else |
| 807 |
{ |
| 808 |
//Bob not found; report the error |
| 809 |
} |
| 810 |
|
| 811 |
//And there's no overhead |
| 812 |
static assert(Nullable!(size_t, size_t.max).sizeof == size_t.sizeof); |
| 813 |
} |
| 814 |
|
| 815 |
@system unittest |
| 816 |
{ |
| 817 |
import std.typecons; |
| 818 |
|
| 819 |
import std.exception : assertThrown; |
| 820 |
|
| 821 |
Nullable!(int, int.min) a; |
| 822 |
assert(a.isNull); |
| 823 |
assertThrown!Throwable(a.get); |
| 824 |
a = 5; |
| 825 |
assert(!a.isNull); |
| 826 |
assert(a == 5); |
| 827 |
static assert(a.sizeof == int.sizeof); |
| 828 |
} |
| 829 |
|
| 830 |
@safe unittest |
| 831 |
{ |
| 832 |
import std.typecons; |
| 833 |
|
| 834 |
auto a = nullable!(int.min)(8); |
| 835 |
assert(a == 8); |
| 836 |
a.nullify(); |
| 837 |
assert(a.isNull); |
| 838 |
} |
| 839 |
|
| 840 |
nothrow pure @nogc @safe unittest |
| 841 |
{ |
| 842 |
import std.typecons; |
| 843 |
|
| 844 |
alias toFloat = i => cast(float) i; |
| 845 |
|
| 846 |
Nullable!int sample; |
| 847 |
|
| 848 |
// apply(null) results in a null `Nullable` of the function's return type. |
| 849 |
Nullable!float f = sample.apply!toFloat; |
| 850 |
assert(sample.isNull && f.isNull); |
| 851 |
|
| 852 |
sample = 3; |
| 853 |
|
| 854 |
// apply(non-null) calls the function and wraps the result in a `Nullable`. |
| 855 |
f = sample.apply!toFloat; |
| 856 |
assert(!sample.isNull && !f.isNull); |
| 857 |
assert(f.get == 3.0f); |
| 858 |
} |
| 859 |
|
| 860 |
nothrow pure @nogc @safe unittest |
| 861 |
{ |
| 862 |
import std.typecons; |
| 863 |
|
| 864 |
alias greaterThree = i => (i > 3) ? i.nullable : Nullable!(typeof(i)).init; |
| 865 |
|
| 866 |
Nullable!int sample; |
| 867 |
|
| 868 |
// when the function already returns a `Nullable`, that `Nullable` is not wrapped. |
| 869 |
auto result = sample.apply!greaterThree; |
| 870 |
assert(sample.isNull && result.isNull); |
| 871 |
|
| 872 |
// The function may decide to return a null `Nullable`. |
| 873 |
sample = 3; |
| 874 |
result = sample.apply!greaterThree; |
| 875 |
assert(!sample.isNull && result.isNull); |
| 876 |
|
| 877 |
// Or it may return a value already wrapped in a `Nullable`. |
| 878 |
sample = 4; |
| 879 |
result = sample.apply!greaterThree; |
| 880 |
assert(!sample.isNull && !result.isNull); |
| 881 |
assert(result.get == 4); |
| 882 |
} |
| 883 |
|
| 884 |
@safe unittest |
| 885 |
{ |
| 886 |
import std.typecons; |
| 887 |
|
| 888 |
NullableRef!int nr = new int(42); |
| 889 |
assert(nr == 42); |
| 890 |
|
| 891 |
int* n = new int(1); |
| 892 |
nr.bind(n); |
| 893 |
assert(nr == 1); |
| 894 |
|
| 895 |
} |
| 896 |
|
| 897 |
@safe unittest |
| 898 |
{ |
| 899 |
import std.typecons; |
| 900 |
|
| 901 |
NullableRef!int nr; |
| 902 |
assert(nr.isNull); |
| 903 |
|
| 904 |
int* n = new int(42); |
| 905 |
nr.bind(n); |
| 906 |
assert(!nr.isNull && nr == 42); |
| 907 |
|
| 908 |
} |
| 909 |
|
| 910 |
@safe unittest |
| 911 |
{ |
| 912 |
import std.typecons; |
| 913 |
|
| 914 |
NullableRef!int nr = new int(42); |
| 915 |
assert(!nr.isNull); |
| 916 |
|
| 917 |
nr.nullify(); |
| 918 |
assert(nr.isNull); |
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
@system unittest |
| 923 |
{ |
| 924 |
import std.typecons; |
| 925 |
|
| 926 |
import std.exception : assertThrown, assertNotThrown; |
| 927 |
|
| 928 |
NullableRef!int nr; |
| 929 |
assert(nr.isNull); |
| 930 |
assertThrown!Throwable(nr = 42); |
| 931 |
|
| 932 |
nr.bind(new int(0)); |
| 933 |
assert(!nr.isNull); |
| 934 |
assertNotThrown!Throwable(nr = 42); |
| 935 |
assert(nr == 42); |
| 936 |
|
| 937 |
} |
| 938 |
|
| 939 |
@system unittest |
| 940 |
{ |
| 941 |
import std.typecons; |
| 942 |
|
| 943 |
import std.exception : assertThrown, assertNotThrown; |
| 944 |
|
| 945 |
NullableRef!int nr; |
| 946 |
//`get` is implicitly called. Will throw |
| 947 |
//an error in non-release mode |
| 948 |
assertThrown!Throwable(nr == 0); |
| 949 |
|
| 950 |
nr.bind(new int(0)); |
| 951 |
assertNotThrown!Throwable(nr == 0); |
| 952 |
|
| 953 |
} |
| 954 |
|
| 955 |
@system unittest |
| 956 |
{ |
| 957 |
import std.typecons; |
| 958 |
|
| 959 |
import std.exception : assertThrown; |
| 960 |
|
| 961 |
int x = 5, y = 7; |
| 962 |
auto a = nullableRef(&x); |
| 963 |
assert(!a.isNull); |
| 964 |
assert(a == 5); |
| 965 |
assert(x == 5); |
| 966 |
a = 42; |
| 967 |
assert(x == 42); |
| 968 |
assert(!a.isNull); |
| 969 |
assert(a == 42); |
| 970 |
a.nullify(); |
| 971 |
assert(x == 42); |
| 972 |
assert(a.isNull); |
| 973 |
assertThrown!Throwable(a.get); |
| 974 |
assertThrown!Throwable(a = 71); |
| 975 |
a.bind(&y); |
| 976 |
assert(a == 7); |
| 977 |
y = 135; |
| 978 |
assert(a == 135); |
| 979 |
} |
| 980 |
|
| 981 |
@system unittest |
| 982 |
{ |
| 983 |
import std.typecons; |
| 984 |
|
| 985 |
import std.math.traits : isNaN; |
| 986 |
|
| 987 |
static abstract class C |
| 988 |
{ |
| 989 |
int m_value; |
| 990 |
this(int v) { m_value = v; } |
| 991 |
int value() @property { return m_value; } |
| 992 |
|
| 993 |
abstract real realValue() @property; |
| 994 |
abstract void doSomething(); |
| 995 |
} |
| 996 |
|
| 997 |
auto c = new BlackHole!C(42); |
| 998 |
assert(c.value == 42); |
| 999 |
|
| 1000 |
// Returns real.init which is NaN |
| 1001 |
assert(c.realValue.isNaN); |
| 1002 |
// Abstract functions are implemented as do-nothing |
| 1003 |
c.doSomething(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
@system unittest |
| 1007 |
{ |
| 1008 |
import std.typecons; |
| 1009 |
|
| 1010 |
import std.exception : assertThrown; |
| 1011 |
|
| 1012 |
static class C |
| 1013 |
{ |
| 1014 |
abstract void notYetImplemented(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
auto c = new WhiteHole!C; |
| 1018 |
assertThrown!NotImplementedError(c.notYetImplemented()); // throws an Error |
| 1019 |
} |
| 1020 |
|
| 1021 |
@system unittest |
| 1022 |
{ |
| 1023 |
import std.typecons; |
| 1024 |
|
| 1025 |
import std.exception : assertThrown; |
| 1026 |
// nothrow |
| 1027 |
{ |
| 1028 |
interface I_1 |
| 1029 |
{ |
| 1030 |
void foo(); |
| 1031 |
void bar() nothrow; |
| 1032 |
} |
| 1033 |
auto o = new WhiteHole!I_1; |
| 1034 |
assertThrown!NotImplementedError(o.foo()); |
| 1035 |
assertThrown!NotImplementedError(o.bar()); |
| 1036 |
} |
| 1037 |
// doc example |
| 1038 |
{ |
| 1039 |
static class C |
| 1040 |
{ |
| 1041 |
abstract void notYetImplemented(); |
| 1042 |
} |
| 1043 |
|
| 1044 |
auto c = new WhiteHole!C; |
| 1045 |
try |
| 1046 |
{ |
| 1047 |
c.notYetImplemented(); |
| 1048 |
assert(0); |
| 1049 |
} |
| 1050 |
catch (Error e) {} |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
@system unittest |
| 1055 |
{ |
| 1056 |
import std.typecons; |
| 1057 |
|
| 1058 |
interface PackageSupplier |
| 1059 |
{ |
| 1060 |
int foo(); |
| 1061 |
int bar(); |
| 1062 |
} |
| 1063 |
|
| 1064 |
static abstract class AbstractFallbackPackageSupplier : PackageSupplier |
| 1065 |
{ |
| 1066 |
protected PackageSupplier default_, fallback; |
| 1067 |
|
| 1068 |
this(PackageSupplier default_, PackageSupplier fallback) |
| 1069 |
{ |
| 1070 |
this.default_ = default_; |
| 1071 |
this.fallback = fallback; |
| 1072 |
} |
| 1073 |
|
| 1074 |
abstract int foo(); |
| 1075 |
abstract int bar(); |
| 1076 |
} |
| 1077 |
|
| 1078 |
template fallback(T, alias func) |
| 1079 |
{ |
| 1080 |
import std.format : format; |
| 1081 |
// for all implemented methods: |
| 1082 |
// - try default first |
| 1083 |
// - only on a failure run & return fallback |
| 1084 |
enum fallback = q{ |
| 1085 |
try |
| 1086 |
{ |
| 1087 |
return default_.%1$s(args); |
| 1088 |
} |
| 1089 |
catch (Exception) |
| 1090 |
{ |
| 1091 |
return fallback.%1$s(args); |
| 1092 |
} |
| 1093 |
}.format(__traits(identifier, func)); |
| 1094 |
} |
| 1095 |
|
| 1096 |
// combines two classes and use the second one as fallback |
| 1097 |
alias FallbackPackageSupplier = AutoImplement!(AbstractFallbackPackageSupplier, fallback); |
| 1098 |
|
| 1099 |
class FailingPackageSupplier : PackageSupplier |
| 1100 |
{ |
| 1101 |
int foo(){ throw new Exception("failure"); } |
| 1102 |
int bar(){ return 2;} |
| 1103 |
} |
| 1104 |
|
| 1105 |
class BackupPackageSupplier : PackageSupplier |
| 1106 |
{ |
| 1107 |
int foo(){ return -1; } |
| 1108 |
int bar(){ return -1;} |
| 1109 |
} |
| 1110 |
|
| 1111 |
auto registry = new FallbackPackageSupplier(new FailingPackageSupplier(), new BackupPackageSupplier()); |
| 1112 |
|
| 1113 |
assert(registry.foo() == -1); |
| 1114 |
assert(registry.bar() == 2); |
| 1115 |
} |
| 1116 |
|
| 1117 |
@system unittest |
| 1118 |
{ |
| 1119 |
import std.typecons; |
| 1120 |
|
| 1121 |
alias BlackHole(Base) = AutoImplement!(Base, generateEmptyFunction); |
| 1122 |
|
| 1123 |
interface I |
| 1124 |
{ |
| 1125 |
int foo(); |
| 1126 |
string bar(); |
| 1127 |
} |
| 1128 |
|
| 1129 |
auto i = new BlackHole!I(); |
| 1130 |
// generateEmptyFunction returns the default value of the return type without doing anything |
| 1131 |
assert(i.foo == 0); |
| 1132 |
assert(i.bar is null); |
| 1133 |
} |
| 1134 |
|
| 1135 |
@system unittest |
| 1136 |
{ |
| 1137 |
import std.typecons; |
| 1138 |
|
| 1139 |
import std.exception : assertThrown; |
| 1140 |
|
| 1141 |
alias WhiteHole(Base) = AutoImplement!(Base, generateAssertTrap); |
| 1142 |
|
| 1143 |
interface I |
| 1144 |
{ |
| 1145 |
int foo(); |
| 1146 |
string bar(); |
| 1147 |
} |
| 1148 |
|
| 1149 |
auto i = new WhiteHole!I(); |
| 1150 |
// generateAssertTrap throws an exception for every unimplemented function of the interface |
| 1151 |
assertThrown!NotImplementedError(i.foo); |
| 1152 |
assertThrown!NotImplementedError(i.bar); |
| 1153 |
} |
| 1154 |
|
| 1155 |
@system unittest |
| 1156 |
{ |
| 1157 |
import std.typecons; |
| 1158 |
|
| 1159 |
interface Quack |
| 1160 |
{ |
| 1161 |
int quack(); |
| 1162 |
@property int height(); |
| 1163 |
} |
| 1164 |
interface Flyer |
| 1165 |
{ |
| 1166 |
@property int height(); |
| 1167 |
} |
| 1168 |
class Duck : Quack |
| 1169 |
{ |
| 1170 |
int quack() { return 1; } |
| 1171 |
@property int height() { return 10; } |
| 1172 |
} |
| 1173 |
class Human |
| 1174 |
{ |
| 1175 |
int quack() { return 2; } |
| 1176 |
@property int height() { return 20; } |
| 1177 |
} |
| 1178 |
|
| 1179 |
Duck d1 = new Duck(); |
| 1180 |
Human h1 = new Human(); |
| 1181 |
|
| 1182 |
interface Refleshable |
| 1183 |
{ |
| 1184 |
int reflesh(); |
| 1185 |
} |
| 1186 |
|
| 1187 |
// does not have structural conformance |
| 1188 |
static assert(!__traits(compiles, d1.wrap!Refleshable)); |
| 1189 |
static assert(!__traits(compiles, h1.wrap!Refleshable)); |
| 1190 |
|
| 1191 |
// strict upcast |
| 1192 |
Quack qd = d1.wrap!Quack; |
| 1193 |
assert(qd is d1); |
| 1194 |
assert(qd.quack() == 1); // calls Duck.quack |
| 1195 |
// strict downcast |
| 1196 |
Duck d2 = qd.unwrap!Duck; |
| 1197 |
assert(d2 is d1); |
| 1198 |
|
| 1199 |
// structural upcast |
| 1200 |
Quack qh = h1.wrap!Quack; |
| 1201 |
assert(qh.quack() == 2); // calls Human.quack |
| 1202 |
// structural downcast |
| 1203 |
Human h2 = qh.unwrap!Human; |
| 1204 |
assert(h2 is h1); |
| 1205 |
|
| 1206 |
// structural upcast (two steps) |
| 1207 |
Quack qx = h1.wrap!Quack; // Human -> Quack |
| 1208 |
Flyer fx = qx.wrap!Flyer; // Quack -> Flyer |
| 1209 |
assert(fx.height == 20); // calls Human.height |
| 1210 |
// structural downcast (two steps) |
| 1211 |
Quack qy = fx.unwrap!Quack; // Flyer -> Quack |
| 1212 |
Human hy = qy.unwrap!Human; // Quack -> Human |
| 1213 |
assert(hy is h1); |
| 1214 |
// structural downcast (one step) |
| 1215 |
Human hz = fx.unwrap!Human; // Flyer -> Human |
| 1216 |
assert(hz is h1); |
| 1217 |
} |
| 1218 |
|
| 1219 |
@system unittest |
| 1220 |
{ |
| 1221 |
import std.typecons; |
| 1222 |
|
| 1223 |
import std.traits : FunctionAttribute, functionAttributes; |
| 1224 |
interface A { int run(); } |
| 1225 |
interface B { int stop(); @property int status(); } |
| 1226 |
class X |
| 1227 |
{ |
| 1228 |
int run() { return 1; } |
| 1229 |
int stop() { return 2; } |
| 1230 |
@property int status() { return 3; } |
| 1231 |
} |
| 1232 |
|
| 1233 |
auto x = new X(); |
| 1234 |
auto ab = x.wrap!(A, B); |
| 1235 |
A a = ab; |
| 1236 |
B b = ab; |
| 1237 |
assert(a.run() == 1); |
| 1238 |
assert(b.stop() == 2); |
| 1239 |
assert(b.status == 3); |
| 1240 |
static assert(functionAttributes!(typeof(ab).status) & FunctionAttribute.property); |
| 1241 |
} |
| 1242 |
|
| 1243 |
@system unittest |
| 1244 |
{ |
| 1245 |
import std.typecons; |
| 1246 |
|
| 1247 |
import core.exception : AssertError; |
| 1248 |
import std.exception : assertThrown; |
| 1249 |
|
| 1250 |
struct Foo |
| 1251 |
{ |
| 1252 |
int a = 42; |
| 1253 |
} |
| 1254 |
|
| 1255 |
SafeRefCounted!(Foo, RefCountedAutoInitialize.yes) rcAuto; |
| 1256 |
SafeRefCounted!(Foo, RefCountedAutoInitialize.no) rcNoAuto; |
| 1257 |
|
| 1258 |
assert(rcAuto.refCountedPayload.a == 42); |
| 1259 |
|
| 1260 |
assertThrown!AssertError(rcNoAuto.refCountedPayload); |
| 1261 |
rcNoAuto.refCountedStore.ensureInitialized; |
| 1262 |
assert(rcNoAuto.refCountedPayload.a == 42); |
| 1263 |
} |
| 1264 |
|
| 1265 |
pure @system nothrow @nogc unittest |
| 1266 |
{ |
| 1267 |
import std.typecons; |
| 1268 |
|
| 1269 |
// A pair of an `int` and a `size_t` - the latter being the |
| 1270 |
// reference count - will be dynamically allocated |
| 1271 |
auto rc1 = SafeRefCounted!int(5); |
| 1272 |
assert(rc1 == 5); |
| 1273 |
// No more allocation, add just one extra reference count |
| 1274 |
auto rc2 = rc1; |
| 1275 |
// Reference semantics |
| 1276 |
rc2 = 42; |
| 1277 |
assert(rc1 == 42); |
| 1278 |
// the pair will be freed when rc1 and rc2 go out of scope |
| 1279 |
} |
| 1280 |
|
| 1281 |
@safe pure nothrow unittest |
| 1282 |
{ |
| 1283 |
import std.typecons; |
| 1284 |
|
| 1285 |
auto rcInt = safeRefCounted(5); |
| 1286 |
assert(rcInt.borrow!(theInt => theInt) == 5); |
| 1287 |
auto sameInt = rcInt; |
| 1288 |
assert(sameInt.borrow!"a" == 5); |
| 1289 |
|
| 1290 |
// using `ref` in the function |
| 1291 |
auto arr = [0, 1, 2, 3, 4, 5, 6]; |
| 1292 |
sameInt.borrow!(ref (x) => arr[x]) = 10; |
| 1293 |
assert(arr == [0, 1, 2, 3, 4, 10, 6]); |
| 1294 |
|
| 1295 |
// modifying the payload via an alias |
| 1296 |
sameInt.borrow!"a*=2"; |
| 1297 |
assert(rcInt.borrow!"a" == 10); |
| 1298 |
} |
| 1299 |
|
| 1300 |
@system unittest |
| 1301 |
{ |
| 1302 |
import std.typecons; |
| 1303 |
|
| 1304 |
static struct File |
| 1305 |
{ |
| 1306 |
static size_t nDestroyed; |
| 1307 |
string name; |
| 1308 |
@disable this(this); // not copyable |
| 1309 |
~this() { name = null; ++nDestroyed; } |
| 1310 |
} |
| 1311 |
|
| 1312 |
auto file = File("name"); |
| 1313 |
assert(file.name == "name"); |
| 1314 |
// file cannot be copied and has unique ownership |
| 1315 |
static assert(!__traits(compiles, {auto file2 = file;})); |
| 1316 |
|
| 1317 |
assert(File.nDestroyed == 0); |
| 1318 |
|
| 1319 |
// make the file ref counted to share ownership |
| 1320 |
// Note: |
| 1321 |
// We write a compound statement (brace-delimited scope) in which all `SafeRefCounted!File` handles are created and deleted. |
| 1322 |
// This allows us to see (after the scope) what happens after all handles have been destroyed. |
| 1323 |
{ |
| 1324 |
// We move the content of `file` to a separate (and heap-allocated) `File` object, |
| 1325 |
// managed-and-accessed via one-or-multiple (initially: one) `SafeRefCounted!File` objects ("handles"). |
| 1326 |
// This "moving": |
| 1327 |
// (1) invokes `file`'s destructor (=> `File.nDestroyed` is incremented from 0 to 1 and `file.name` becomes `null`); |
| 1328 |
// (2) overwrites `file` with `File.init` (=> `file.name` becomes `null`). |
| 1329 |
// It appears that writing `name = null;` in the destructor is redundant, |
| 1330 |
// but please note that (2) is only performed if `File` defines a destructor (or post-blit operator), |
| 1331 |
// and in the absence of the `nDestroyed` instrumentation there would have been no reason to define a destructor. |
| 1332 |
import std.algorithm.mutation : move; |
| 1333 |
auto rcFile = safeRefCounted(move(file)); |
| 1334 |
assert(rcFile.name == "name"); |
| 1335 |
assert(File.nDestroyed == 1); |
| 1336 |
assert(file.name == null); |
| 1337 |
|
| 1338 |
// We create another `SafeRefCounted!File` handle to the same separate `File` object. |
| 1339 |
// While any of the handles is still alive, the `File` object is kept alive (=> `File.nDestroyed` is not modified). |
| 1340 |
auto rcFile2 = rcFile; |
| 1341 |
assert(rcFile.refCountedStore.refCount == 2); |
| 1342 |
assert(File.nDestroyed == 1); |
| 1343 |
} |
| 1344 |
// The separate `File` object is deleted when the last `SafeRefCounted!File` handle is destroyed |
| 1345 |
// (i.e. at the closing brace of the compound statement above, which destroys both handles: `rcFile` and `rcFile2`) |
| 1346 |
// (=> `File.nDestroyed` is incremented again, from 1 to 2): |
| 1347 |
assert(File.nDestroyed == 2); |
| 1348 |
} |
| 1349 |
|
| 1350 |
@safe unittest |
| 1351 |
{ |
| 1352 |
import std.typecons; |
| 1353 |
|
| 1354 |
struct MyInt |
| 1355 |
{ |
| 1356 |
private int value; |
| 1357 |
mixin Proxy!value; |
| 1358 |
|
| 1359 |
this(int n){ value = n; } |
| 1360 |
} |
| 1361 |
|
| 1362 |
MyInt n = 10; |
| 1363 |
|
| 1364 |
// Enable operations that original type has. |
| 1365 |
++n; |
| 1366 |
assert(n == 11); |
| 1367 |
assert(n * 2 == 22); |
| 1368 |
|
| 1369 |
void func(int n) { } |
| 1370 |
|
| 1371 |
// Disable implicit conversions to original type. |
| 1372 |
//int x = n; |
| 1373 |
//func(n); |
| 1374 |
} |
| 1375 |
|
| 1376 |
@safe unittest |
| 1377 |
{ |
| 1378 |
import std.typecons; |
| 1379 |
|
| 1380 |
struct NewIntType |
| 1381 |
{ |
| 1382 |
//Won't work; the literal '1' |
| 1383 |
//is an rvalue, not an lvalue |
| 1384 |
//mixin Proxy!1; |
| 1385 |
|
| 1386 |
//Okay, n is an lvalue |
| 1387 |
int n; |
| 1388 |
mixin Proxy!n; |
| 1389 |
|
| 1390 |
this(int n) { this.n = n; } |
| 1391 |
} |
| 1392 |
|
| 1393 |
NewIntType nit = 0; |
| 1394 |
nit++; |
| 1395 |
assert(nit == 1); |
| 1396 |
|
| 1397 |
|
| 1398 |
struct NewObjectType |
| 1399 |
{ |
| 1400 |
Object obj; |
| 1401 |
//Ok, obj is an lvalue |
| 1402 |
mixin Proxy!obj; |
| 1403 |
|
| 1404 |
this (Object o) { obj = o; } |
| 1405 |
} |
| 1406 |
|
| 1407 |
NewObjectType not = new Object(); |
| 1408 |
assert(__traits(compiles, not.toHash())); |
| 1409 |
} |
| 1410 |
|
| 1411 |
@safe unittest |
| 1412 |
{ |
| 1413 |
import std.typecons; |
| 1414 |
|
| 1415 |
import std.math.traits : isInfinity; |
| 1416 |
|
| 1417 |
float f = 1.0; |
| 1418 |
assert(!f.isInfinity); |
| 1419 |
|
| 1420 |
struct NewFloat |
| 1421 |
{ |
| 1422 |
float _; |
| 1423 |
mixin Proxy!_; |
| 1424 |
|
| 1425 |
this(float f) { _ = f; } |
| 1426 |
} |
| 1427 |
|
| 1428 |
NewFloat nf = 1.0f; |
| 1429 |
assert(!nf.isInfinity); |
| 1430 |
} |
| 1431 |
|
| 1432 |
@safe unittest |
| 1433 |
{ |
| 1434 |
import std.typecons; |
| 1435 |
|
| 1436 |
import std.conv : to; |
| 1437 |
|
| 1438 |
int i = 123; |
| 1439 |
auto td = Typedef!int(i); |
| 1440 |
assert(i.to!string == td.to!string); |
| 1441 |
|
| 1442 |
} |
| 1443 |
|
| 1444 |
@safe unittest |
| 1445 |
{ |
| 1446 |
import std.typecons; |
| 1447 |
|
| 1448 |
alias MyInt = Typedef!int; |
| 1449 |
MyInt foo = 10; |
| 1450 |
foo++; |
| 1451 |
assert(foo == 11); |
| 1452 |
} |
| 1453 |
|
| 1454 |
@safe unittest |
| 1455 |
{ |
| 1456 |
import std.typecons; |
| 1457 |
|
| 1458 |
alias MyIntInit = Typedef!(int, 42); |
| 1459 |
static assert(is(TypedefType!MyIntInit == int)); |
| 1460 |
static assert(MyIntInit() == 42); |
| 1461 |
} |
| 1462 |
|
| 1463 |
@safe unittest |
| 1464 |
{ |
| 1465 |
import std.typecons; |
| 1466 |
|
| 1467 |
alias MyInt = Typedef!int; |
| 1468 |
static void takeInt(int) {} |
| 1469 |
static void takeMyInt(MyInt) {} |
| 1470 |
|
| 1471 |
int i; |
| 1472 |
takeInt(i); // ok |
| 1473 |
static assert(!__traits(compiles, takeMyInt(i))); |
| 1474 |
|
| 1475 |
MyInt myInt; |
| 1476 |
static assert(!__traits(compiles, takeInt(myInt))); |
| 1477 |
takeMyInt(myInt); // ok |
| 1478 |
} |
| 1479 |
|
| 1480 |
@safe unittest |
| 1481 |
{ |
| 1482 |
import std.typecons; |
| 1483 |
|
| 1484 |
alias TypeInt1 = Typedef!int; |
| 1485 |
alias TypeInt2 = Typedef!int; |
| 1486 |
|
| 1487 |
// The two Typedefs are the same type. |
| 1488 |
static assert(is(TypeInt1 == TypeInt2)); |
| 1489 |
|
| 1490 |
alias MoneyEuros = Typedef!(float, float.init, "euros"); |
| 1491 |
alias MoneyDollars = Typedef!(float, float.init, "dollars"); |
| 1492 |
|
| 1493 |
// The two Typedefs are _not_ the same type. |
| 1494 |
static assert(!is(MoneyEuros == MoneyDollars)); |
| 1495 |
} |
| 1496 |
|
| 1497 |
@safe unittest |
| 1498 |
{ |
| 1499 |
import std.typecons; |
| 1500 |
|
| 1501 |
import std.conv : to; |
| 1502 |
|
| 1503 |
alias MyInt = Typedef!int; |
| 1504 |
static assert(is(TypedefType!MyInt == int)); |
| 1505 |
|
| 1506 |
/// Instantiating with a non-Typedef will return that type |
| 1507 |
static assert(is(TypedefType!int == int)); |
| 1508 |
|
| 1509 |
string num = "5"; |
| 1510 |
|
| 1511 |
// extract the needed type |
| 1512 |
MyInt myInt = MyInt( num.to!(TypedefType!MyInt) ); |
| 1513 |
assert(myInt == 5); |
| 1514 |
|
| 1515 |
// cast to the underlying type to get the value that's being wrapped |
| 1516 |
int x = cast(TypedefType!MyInt) myInt; |
| 1517 |
|
| 1518 |
alias MyIntInit = Typedef!(int, 42); |
| 1519 |
static assert(is(TypedefType!MyIntInit == int)); |
| 1520 |
static assert(MyIntInit() == 42); |
| 1521 |
} |
| 1522 |
|
| 1523 |
@system unittest |
| 1524 |
{ |
| 1525 |
import std.typecons; |
| 1526 |
|
| 1527 |
class A |
| 1528 |
{ |
| 1529 |
int x; |
| 1530 |
this() {x = 0;} |
| 1531 |
this(int i){x = i;} |
| 1532 |
~this() {} |
| 1533 |
} |
| 1534 |
|
| 1535 |
// Standard usage, constructing A on the stack |
| 1536 |
auto a1 = scoped!A(); |
| 1537 |
a1.x = 42; |
| 1538 |
|
| 1539 |
// Result of `scoped` call implicitly converts to a class reference |
| 1540 |
A aRef = a1; |
| 1541 |
assert(aRef.x == 42); |
| 1542 |
|
| 1543 |
// Scoped destruction |
| 1544 |
{ |
| 1545 |
auto a2 = scoped!A(1); |
| 1546 |
assert(a2.x == 1); |
| 1547 |
aRef = a2; |
| 1548 |
// a2 is destroyed here, calling A's destructor |
| 1549 |
} |
| 1550 |
// aRef is now an invalid reference |
| 1551 |
|
| 1552 |
// Here the temporary scoped A is immediately destroyed. |
| 1553 |
// This means the reference is then invalid. |
| 1554 |
version (Bug) |
| 1555 |
{ |
| 1556 |
// Wrong, should use `auto` |
| 1557 |
A invalid = scoped!A(); |
| 1558 |
} |
| 1559 |
|
| 1560 |
// Restrictions |
| 1561 |
version (Bug) |
| 1562 |
{ |
| 1563 |
import std.algorithm.mutation : move; |
| 1564 |
auto invalid = a1.move; // illegal, scoped objects can't be moved |
| 1565 |
} |
| 1566 |
static assert(!is(typeof({ |
| 1567 |
auto e1 = a1; // illegal, scoped objects can't be copied |
| 1568 |
assert([a1][0].x == 42); // ditto |
| 1569 |
}))); |
| 1570 |
static assert(!is(typeof({ |
| 1571 |
alias ScopedObject = typeof(a1); |
| 1572 |
auto e2 = ScopedObject(); // illegal, must be built via scoped!A |
| 1573 |
auto e3 = ScopedObject(1); // ditto |
| 1574 |
}))); |
| 1575 |
|
| 1576 |
// Use with alias |
| 1577 |
alias makeScopedA = scoped!A; |
| 1578 |
auto a3 = makeScopedA(); |
| 1579 |
auto a4 = makeScopedA(1); |
| 1580 |
|
| 1581 |
// Use as member variable |
| 1582 |
struct B |
| 1583 |
{ |
| 1584 |
typeof(scoped!A()) a; // note the trailing parentheses |
| 1585 |
|
| 1586 |
this(int i) |
| 1587 |
{ |
| 1588 |
// construct member |
| 1589 |
a = scoped!A(i); |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
// Stack-allocate |
| 1594 |
auto b1 = B(5); |
| 1595 |
aRef = b1.a; |
| 1596 |
assert(aRef.x == 5); |
| 1597 |
destroy(b1); // calls A's destructor for b1.a |
| 1598 |
// aRef is now an invalid reference |
| 1599 |
|
| 1600 |
// Heap-allocate |
| 1601 |
auto b2 = new B(6); |
| 1602 |
assert(b2.a.x == 6); |
| 1603 |
destroy(*b2); // calls A's destructor for b2.a |
| 1604 |
} |
| 1605 |
|
| 1606 |
@safe unittest |
| 1607 |
{ |
| 1608 |
import std.typecons; |
| 1609 |
|
| 1610 |
Flag!"abc" flag; |
| 1611 |
|
| 1612 |
assert(flag == Flag!"abc".no); |
| 1613 |
assert(flag == No.abc); |
| 1614 |
assert(!flag); |
| 1615 |
if (flag) assert(0); |
| 1616 |
} |
| 1617 |
|
| 1618 |
@safe unittest |
| 1619 |
{ |
| 1620 |
import std.typecons; |
| 1621 |
|
| 1622 |
auto flag = Yes.abc; |
| 1623 |
|
| 1624 |
assert(flag); |
| 1625 |
assert(flag == Yes.abc); |
| 1626 |
if (!flag) assert(0); |
| 1627 |
if (flag) {} else assert(0); |
| 1628 |
} |
| 1629 |
|
| 1630 |
@safe unittest |
| 1631 |
{ |
| 1632 |
import std.typecons; |
| 1633 |
|
| 1634 |
Flag!"abc" flag; |
| 1635 |
|
| 1636 |
assert(flag == Flag!"abc".no); |
| 1637 |
assert(flag == No.abc); |
| 1638 |
assert(!flag); |
| 1639 |
if (flag) assert(0); |
| 1640 |
} |
| 1641 |
|
| 1642 |
@safe unittest |
| 1643 |
{ |
| 1644 |
import std.typecons; |
| 1645 |
|
| 1646 |
auto flag = Yes.abc; |
| 1647 |
|
| 1648 |
assert(flag); |
| 1649 |
assert(flag == Yes.abc); |
| 1650 |
if (!flag) assert(0); |
| 1651 |
if (flag) {} else assert(0); |
| 1652 |
} |
| 1653 |
|
| 1654 |
@safe pure nothrow unittest |
| 1655 |
{ |
| 1656 |
import std.typecons; |
| 1657 |
|
| 1658 |
enum A |
| 1659 |
{ |
| 1660 |
None, |
| 1661 |
A = 1 << 0, |
| 1662 |
B = 1 << 1, |
| 1663 |
C = 1 << 2, |
| 1664 |
D = 1 << 3, |
| 1665 |
} |
| 1666 |
|
| 1667 |
static assert(isBitFlagEnum!A); |
| 1668 |
} |
| 1669 |
|
| 1670 |
@safe pure nothrow unittest |
| 1671 |
{ |
| 1672 |
import std.typecons; |
| 1673 |
|
| 1674 |
enum B |
| 1675 |
{ |
| 1676 |
A, |
| 1677 |
B, |
| 1678 |
C, |
| 1679 |
D // D == 3 |
| 1680 |
} |
| 1681 |
|
| 1682 |
static assert(!isBitFlagEnum!B); |
| 1683 |
} |
| 1684 |
|
| 1685 |
@safe pure nothrow unittest |
| 1686 |
{ |
| 1687 |
import std.typecons; |
| 1688 |
|
| 1689 |
enum C: double |
| 1690 |
{ |
| 1691 |
A = 1 << 0, |
| 1692 |
B = 1 << 1 |
| 1693 |
} |
| 1694 |
|
| 1695 |
static assert(!isBitFlagEnum!C); |
| 1696 |
} |
| 1697 |
|
| 1698 |
@safe @nogc pure nothrow unittest |
| 1699 |
{ |
| 1700 |
import std.typecons; |
| 1701 |
|
| 1702 |
enum Enum |
| 1703 |
{ |
| 1704 |
A = 1 << 0, |
| 1705 |
} |
| 1706 |
|
| 1707 |
// A default constructed BitFlags has no value set |
| 1708 |
immutable BitFlags!Enum flags_empty; |
| 1709 |
assert(!flags_empty.A); |
| 1710 |
|
| 1711 |
// Value can be set with the | operator |
| 1712 |
immutable flags_A = flags_empty | Enum.A; |
| 1713 |
|
| 1714 |
// and tested using property access |
| 1715 |
assert(flags_A.A); |
| 1716 |
|
| 1717 |
// or the & operator |
| 1718 |
assert(flags_A & Enum.A); |
| 1719 |
// which commutes. |
| 1720 |
assert(Enum.A & flags_A); |
| 1721 |
} |
| 1722 |
|
| 1723 |
@safe @nogc pure nothrow unittest |
| 1724 |
{ |
| 1725 |
import std.typecons; |
| 1726 |
|
| 1727 |
enum Enum |
| 1728 |
{ |
| 1729 |
None, |
| 1730 |
A = 1 << 0, |
| 1731 |
B = 1 << 1, |
| 1732 |
C = 1 << 2 |
| 1733 |
} |
| 1734 |
|
| 1735 |
immutable BitFlags!Enum flags_empty; |
| 1736 |
assert(!(flags_empty & (Enum.A | Enum.B | Enum.C))); |
| 1737 |
assert(!(flags_empty & Enum.A) && !(flags_empty & Enum.B) && !(flags_empty & Enum.C)); |
| 1738 |
} |
| 1739 |
|
| 1740 |
@safe @nogc pure nothrow unittest |
| 1741 |
{ |
| 1742 |
import std.typecons; |
| 1743 |
|
| 1744 |
enum Enum |
| 1745 |
{ |
| 1746 |
A = 1 << 0, |
| 1747 |
B = 1 << 1, |
| 1748 |
C = 1 << 2, |
| 1749 |
} |
| 1750 |
immutable BitFlags!Enum flags_AB = BitFlags!Enum(Enum.A, Enum.B); |
| 1751 |
immutable BitFlags!Enum flags_BC = BitFlags!Enum(Enum.B, Enum.C); |
| 1752 |
|
| 1753 |
// Use the ~ operator for subtracting flags |
| 1754 |
immutable BitFlags!Enum flags_B = flags_AB & ~BitFlags!Enum(Enum.A); |
| 1755 |
assert(!flags_B.A && flags_B.B && !flags_B.C); |
| 1756 |
|
| 1757 |
// use & between BitFlags for intersection |
| 1758 |
assert(flags_B == (flags_BC & flags_AB)); |
| 1759 |
} |
| 1760 |
|
| 1761 |
@safe @nogc pure nothrow unittest |
| 1762 |
{ |
| 1763 |
import std.typecons; |
| 1764 |
|
| 1765 |
enum Enum |
| 1766 |
{ |
| 1767 |
A = 1 << 0, |
| 1768 |
B = 1 << 1, |
| 1769 |
} |
| 1770 |
|
| 1771 |
BitFlags!Enum flags_empty, temp, flags_AB; |
| 1772 |
flags_AB = Enum.A | Enum.B; |
| 1773 |
|
| 1774 |
temp |= flags_AB; |
| 1775 |
assert(temp == (flags_empty | flags_AB)); |
| 1776 |
|
| 1777 |
temp = flags_empty; |
| 1778 |
temp |= Enum.B; |
| 1779 |
assert(temp == (flags_empty | Enum.B)); |
| 1780 |
|
| 1781 |
temp = flags_empty; |
| 1782 |
temp &= flags_AB; |
| 1783 |
assert(temp == (flags_empty & flags_AB)); |
| 1784 |
|
| 1785 |
temp = flags_empty; |
| 1786 |
temp &= Enum.A; |
| 1787 |
assert(temp == (flags_empty & Enum.A)); |
| 1788 |
} |
| 1789 |
|
| 1790 |
@safe @nogc pure nothrow unittest |
| 1791 |
{ |
| 1792 |
import std.typecons; |
| 1793 |
|
| 1794 |
enum Enum |
| 1795 |
{ |
| 1796 |
A = 1 << 0, |
| 1797 |
B = 1 << 1, |
| 1798 |
} |
| 1799 |
|
| 1800 |
BitFlags!Enum flags; |
| 1801 |
|
| 1802 |
// BitFlags with no value set evaluate to false |
| 1803 |
assert(!flags); |
| 1804 |
|
| 1805 |
// BitFlags with at least one value set evaluate to true |
| 1806 |
flags |= Enum.A; |
| 1807 |
assert(flags); |
| 1808 |
|
| 1809 |
// This can be useful to check intersection between BitFlags |
| 1810 |
BitFlags!Enum flags_AB = Enum.A | Enum.B; |
| 1811 |
assert(flags & flags_AB); |
| 1812 |
assert(flags & Enum.A); |
| 1813 |
|
| 1814 |
// You can of course get you raw value out of flags |
| 1815 |
auto value = cast(int) flags; |
| 1816 |
assert(value == Enum.A); |
| 1817 |
} |
| 1818 |
|
| 1819 |
@safe @nogc pure nothrow unittest |
| 1820 |
{ |
| 1821 |
import std.typecons; |
| 1822 |
|
| 1823 |
enum UnsafeEnum |
| 1824 |
{ |
| 1825 |
A = 1, |
| 1826 |
B = 2, |
| 1827 |
C = 4, |
| 1828 |
BC = B|C |
| 1829 |
} |
| 1830 |
static assert(!__traits(compiles, { BitFlags!UnsafeEnum flags; })); |
| 1831 |
BitFlags!(UnsafeEnum, Yes.unsafe) flags; |
| 1832 |
|
| 1833 |
// property access tests for exact match of unsafe enums |
| 1834 |
flags.B = true; |
| 1835 |
assert(!flags.BC); // only B |
| 1836 |
flags.C = true; |
| 1837 |
assert(flags.BC); // both B and C |
| 1838 |
flags.B = false; |
| 1839 |
assert(!flags.BC); // only C |
| 1840 |
|
| 1841 |
// property access sets all bits of unsafe enum group |
| 1842 |
flags = flags.init; |
| 1843 |
flags.BC = true; |
| 1844 |
assert(!flags.A && flags.B && flags.C); |
| 1845 |
flags.A = true; |
| 1846 |
flags.BC = false; |
| 1847 |
assert(flags.A && !flags.B && !flags.C); |
| 1848 |
} |
| 1849 |
|
| 1850 |
@safe unittest |
| 1851 |
{ |
| 1852 |
import std.typecons; |
| 1853 |
|
| 1854 |
static assert( |
| 1855 |
is(ReplaceType!(int, string, int[]) == string[]) && |
| 1856 |
is(ReplaceType!(int, string, int[int]) == string[string]) && |
| 1857 |
is(ReplaceType!(int, string, const(int)[]) == const(string)[]) && |
| 1858 |
is(ReplaceType!(int, string, Tuple!(int[], float)) |
| 1859 |
== Tuple!(string[], float)) |
| 1860 |
); |
| 1861 |
} |
| 1862 |
|
| 1863 |
@safe unittest |
| 1864 |
{ |
| 1865 |
import std.typecons; |
| 1866 |
|
| 1867 |
import std.traits : isArray; |
| 1868 |
|
| 1869 |
static assert( |
| 1870 |
is(ReplaceTypeUnless!(isArray, int, string, int*) == string*) && |
| 1871 |
is(ReplaceTypeUnless!(isArray, int, string, int[]) == int[]) && |
| 1872 |
is(ReplaceTypeUnless!(isArray, int, string, Tuple!(int, int[])) |
| 1873 |
== Tuple!(string, int[])) |
| 1874 |
); |
| 1875 |
} |
| 1876 |
|
| 1877 |
@safe @nogc nothrow pure unittest |
| 1878 |
{ |
| 1879 |
import std.typecons; |
| 1880 |
|
| 1881 |
Ternary a; |
| 1882 |
assert(a == Ternary.unknown); |
| 1883 |
|
| 1884 |
assert(~Ternary.yes == Ternary.no); |
| 1885 |
assert(~Ternary.no == Ternary.yes); |
| 1886 |
assert(~Ternary.unknown == Ternary.unknown); |
| 1887 |
} |
| 1888 |
|
| 1889 |
pure @system nothrow @nogc unittest |
| 1890 |
{ |
| 1891 |
import std.typecons; |
| 1892 |
|
| 1893 |
auto rc1 = RefCounted!int(5); |
| 1894 |
assert(rc1 == 5); |
| 1895 |
auto rc2 = rc1; |
| 1896 |
rc2 = 42; |
| 1897 |
assert(rc1 == 42); |
| 1898 |
} |
| 1899 |
|
| 1900 |
@system unittest |
| 1901 |
{ |
| 1902 |
import std.typecons; |
| 1903 |
|
| 1904 |
static struct File |
| 1905 |
{ |
| 1906 |
static size_t nDestroyed; |
| 1907 |
string name; |
| 1908 |
@disable this(this); // not copyable |
| 1909 |
~this() { name = null; ++nDestroyed; } |
| 1910 |
} |
| 1911 |
|
| 1912 |
auto file = File("name"); |
| 1913 |
assert(file.name == "name"); |
| 1914 |
static assert(!__traits(compiles, {auto file2 = file;})); |
| 1915 |
assert(File.nDestroyed == 0); |
| 1916 |
|
| 1917 |
{ |
| 1918 |
import std.algorithm.mutation : move; |
| 1919 |
auto rcFile = refCounted(move(file)); |
| 1920 |
assert(rcFile.name == "name"); |
| 1921 |
assert(File.nDestroyed == 1); |
| 1922 |
assert(file.name == null); |
| 1923 |
|
| 1924 |
auto rcFile2 = rcFile; |
| 1925 |
assert(rcFile.refCountedStore.refCount == 2); |
| 1926 |
assert(File.nDestroyed == 1); |
| 1927 |
} |
| 1928 |
|
| 1929 |
assert(File.nDestroyed == 2); |
| 1930 |
} |
| 1931 |
|