mirror of https://git.sr.ht/~stygianentity/bincode
Added support for the bool type
This commit is contained in:
parent
c4cb220fb2
commit
e4e12c984b
|
|
@ -1,6 +1,16 @@
|
|||
use super::{BorrowDecodable, BorrowDecode, Decodable, Decode};
|
||||
use crate::error::DecodeError;
|
||||
|
||||
impl<'de> Decodable for bool {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
match decoder.decode_u8()? {
|
||||
0 => Ok(false),
|
||||
1 => Ok(true),
|
||||
x => Err(DecodeError::InvalidBooleanValue(x)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for u8 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_u8()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
use super::{Encode, Encodeable};
|
||||
use crate::error::EncodeError;
|
||||
|
||||
impl Encodeable for bool {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_u8(if *self { 1 } else { 0 })
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for u8 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_u8(*self)
|
||||
|
|
|
|||
|
|
@ -49,6 +49,9 @@ pub enum DecodeError {
|
|||
|
||||
/// The decoder tried to decode a `char` and failed. The given buffer contains the bytes that are read at the moment of failure.
|
||||
InvalidCharEncoding([u8; 4]),
|
||||
|
||||
/// The decoder tried to decode a `bool` and failed. The given value is what is actually read.
|
||||
InvalidBooleanValue(u8),
|
||||
}
|
||||
|
||||
/// Integer types. Used by [DecodeError]. These types have no purpose other than being shown in errors.
|
||||
|
|
|
|||
|
|
@ -75,6 +75,9 @@ fn test_numbers() {
|
|||
the_same(5.0f32);
|
||||
the_same(5.0f64);
|
||||
|
||||
the_same(true);
|
||||
the_same(false);
|
||||
|
||||
for char in "aÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö文".chars()
|
||||
{
|
||||
the_same(char);
|
||||
|
|
|
|||
Loading…
Reference in New Issue