Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a to_string method for Encoding enum #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt;

use crate::parse;
extern crate alloc;
use alloc::string::String;

/// An Objective-C type encoding.
///
Expand Down Expand Up @@ -35,6 +36,17 @@ pub enum Encoding<'a> {
Union(&'a str, &'a [Encoding<'a>]),
}

impl Encoding<'_> {
fn to_string(&self) -> String {
let mut buf = String::new();
let mut formatter = fmt::Formatter::new(&mut buf);
// Bypass format_args!() to avoid write_str with zero-length strs
fmt::Display::fmt(self, &mut formatter)
.expect("a Display implementation returned an error unexpectedly");
buf
}
}

impl fmt::Display for Encoding<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
use Encoding::*;
Expand Down Expand Up @@ -89,19 +101,18 @@ impl fmt::Display for Encoding<'_> {

impl PartialEq<str> for Encoding<'_> {
fn eq(&self, other: &str) -> bool {
parse::eq_enc(other, self)
self.to_string() == other
}
}

impl PartialEq<Encoding<'_>> for str {
fn eq(&self, other: &Encoding) -> bool {
parse::eq_enc(self, other)
self == other.to_string()
}
}

#[cfg(test)]
mod tests {
use std::string::ToString;
use super::Encoding;

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ assert_eq!(i32::ENCODING.to_string(), "i");
*/

#![no_std]

#![feature(fmt_internals)]
#[cfg(test)]
extern crate std;

mod encoding;
mod encode;
mod parse;

pub use crate::encoding::Encoding;
pub use crate::encode::Encode;
146 changes: 0 additions & 146 deletions src/parse.rs

This file was deleted.