-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid overflow exceptions when converting
BOOL
to bool
This means it has to be a lossy conversion, since `BOOL` is 4 bytes and `bool` is 1 byte. Generally .NET recommends avoiding lossy implicity operators (though explicit is ok). But for a type like this, it seems like super-high value for folks to be able to author `if (!SomeBOOL())` and have that work without casts. Fixes #624
- Loading branch information
Showing
4 changed files
with
82 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
partial struct BOOL | ||
{ | ||
internal unsafe BOOL(bool value) => this.Value = *(sbyte*)&value; | ||
public static unsafe implicit operator bool(BOOL value) | ||
{ | ||
sbyte v = checked((sbyte)value.Value); | ||
return *(bool*)&v; | ||
} | ||
|
||
internal BOOL(bool value) => this.Value = value ? 1 : 0; | ||
public static implicit operator bool(BOOL value) => value.Value != 0; | ||
public static implicit operator BOOL(bool value) => new BOOL(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
partial struct BOOLEAN | ||
{ | ||
internal unsafe BOOLEAN(bool value) => this.Value = *(byte*)&value; | ||
public static unsafe implicit operator bool(BOOLEAN value) | ||
{ | ||
byte v = checked((byte)value.Value); | ||
return *(bool*)&v; | ||
} | ||
|
||
internal BOOLEAN(bool value) => this.Value = value ? (byte)1 : (byte)0; | ||
public static implicit operator bool(BOOLEAN value) => value.Value != 0; | ||
public static implicit operator BOOLEAN(bool value) => new BOOLEAN(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters