Skip to content

Commit

Permalink
remove 'FromU16' trait
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Nov 22, 2022
1 parent 8f1cad4 commit 3df12bf
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 61 deletions.
2 changes: 1 addition & 1 deletion benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "benchmark"
version = "0.0.0"
authors = [ "David Renshaw <dwrenshaw@sandstorm.io>" ]
build = "build.rs"
edition = "2018"
edition = "2021"

[[bin]]

Expand Down
3 changes: 2 additions & 1 deletion benchmark/carsales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ pub fn random_car(rng: &mut FastRand, mut car: car::Builder) {
car.set_model(MODELS[rng.next_less_than(MODELS.len() as u32) as usize]);

car.set_color(
::capnp::traits::FromU16::from_u16(rng.next_less_than(Color::Silver as u32 + 1) as u16)
(rng.next_less_than(Color::Silver as u32 + 1) as u16)
.try_into()
.unwrap(),
);
car.set_seats(2 + rng.next_less_than(6) as u8);
Expand Down
6 changes: 5 additions & 1 deletion benchmark/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ use crate::common::*;
use crate::eval_capnp::{evaluation_result, expression, Operation};

fn make_expression(rng: &mut FastRand, mut exp: expression::Builder, depth: u32) -> i32 {
exp.set_op(::capnp::traits::FromU16::from_u16(rng.next_less_than( Operation::Modulus as u32 + 1) as u16).unwrap());
exp.set_op(
(rng.next_less_than(Operation::Modulus as u32 + 1) as u16)
.try_into()
.unwrap(),
);

let left: i32 = if rng.next_less_than(8) < depth {
let tmp = (rng.next_less_than(128) + 1) as i32;
Expand Down
2 changes: 0 additions & 2 deletions capnp-futures/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
//! Asynchronous reading and writing of messages using the
//! [standard stream framing](https://capnproto.org/encoding.html#serialization-over-a-stream).
use std::convert::TryInto;

use capnp::serialize::{OwnedSegments, SegmentLengthsBuilder};
use capnp::{message, Error, OutputSegments, Result};

Expand Down
10 changes: 5 additions & 5 deletions capnp-rpc/src/rpc_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3807,7 +3807,7 @@ pub mod exception {
}
#[inline]
pub fn get_type(self) -> ::core::result::Result<crate::rpc_capnp::exception::Type,::capnp::NotInSchema> {
::capnp::traits::FromU16::from_u16(self.reader.get_data_field::<u16>(2))
::core::convert::TryInto::try_into(self.reader.get_data_field::<u16>(2))
}
#[inline]
pub fn get_trace(self) -> ::capnp::Result<::capnp::text::Reader<'a>> {
Expand Down Expand Up @@ -3899,7 +3899,7 @@ pub mod exception {
}
#[inline]
pub fn get_type(self) -> ::core::result::Result<crate::rpc_capnp::exception::Type,::capnp::NotInSchema> {
::capnp::traits::FromU16::from_u16(self.builder.get_data_field::<u16>(2))
::core::convert::TryInto::try_into(self.builder.get_data_field::<u16>(2))
}
#[inline]
pub fn set_type(&mut self, value: crate::rpc_capnp::exception::Type) {
Expand Down Expand Up @@ -3943,9 +3943,9 @@ pub mod exception {
Disconnected = 2,
Unimplemented = 3,
}
impl ::capnp::traits::FromU16 for Type {
#[inline]
fn from_u16(value: u16) -> ::core::result::Result<Self, ::capnp::NotInSchema> {
impl ::core::convert::TryFrom<u16> for Type {
type Error = ::capnp::NotInSchema;
fn try_from(value: u16) -> ::core::result::Result<Self, Self::Error> {
match value {
0 => ::core::result::Result::Ok(Self::Failed),
1 => ::core::result::Result::Ok(Self::Overloaded),
Expand Down
10 changes: 5 additions & 5 deletions capnp-rpc/src/rpc_twoparty_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub enum Side {
Server = 0,
Client = 1,
}
impl ::capnp::traits::FromU16 for Side {
#[inline]
fn from_u16(value: u16) -> ::core::result::Result<Self, ::capnp::NotInSchema> {
impl ::core::convert::TryFrom<u16> for Side {
type Error = ::capnp::NotInSchema;
fn try_from(value: u16) -> ::core::result::Result<Self, Self::Error> {
match value {
0 => ::core::result::Result::Ok(Self::Server),
1 => ::core::result::Result::Ok(Self::Client),
Expand Down Expand Up @@ -74,7 +74,7 @@ pub mod vat_id {
}
#[inline]
pub fn get_side(self) -> ::core::result::Result<crate::rpc_twoparty_capnp::Side,::capnp::NotInSchema> {
::capnp::traits::FromU16::from_u16(self.reader.get_data_field::<u16>(0))
::core::convert::TryInto::try_into(self.reader.get_data_field::<u16>(0))
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ pub mod vat_id {
}
#[inline]
pub fn get_side(self) -> ::core::result::Result<crate::rpc_twoparty_capnp::Side,::capnp::NotInSchema> {
::capnp::traits::FromU16::from_u16(self.builder.get_data_field::<u16>(0))
::core::convert::TryInto::try_into(self.builder.get_data_field::<u16>(0))
}
#[inline]
pub fn set_side(&mut self, value: crate::rpc_twoparty_capnp::Side) {
Expand Down
30 changes: 16 additions & 14 deletions capnp/src/enum_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use crate::private::layout::{
ListBuilder, ListReader, PointerBuilder, PointerReader, PrimitiveElement, TwoBytes,
};
use crate::traits::{FromPointerBuilder, FromPointerReader, FromU16, IndexMove, ListIter, ToU16};
use crate::traits::{FromPointerBuilder, FromPointerReader, IndexMove, ListIter, ToU16};
use crate::{NotInSchema, Result};

use core::marker::PhantomData;
Expand All @@ -36,7 +36,7 @@ pub struct Owned<T> {

impl<T> crate::traits::Owned for Owned<T>
where
T: FromU16,
T: TryFrom<u16, Error = NotInSchema>,
{
type Reader<'a> = Reader<'a, T>;
type Builder<'a> = Builder<'a, T>;
Expand All @@ -48,7 +48,7 @@ pub struct Reader<'a, T> {
reader: ListReader<'a>,
}

impl<'a, T: FromU16> Reader<'a, T> {
impl<'a, T: TryFrom<u16, Error = NotInSchema>> Reader<'a, T> {
pub fn len(&self) -> u32 {
self.reader.len()
}
Expand All @@ -63,7 +63,7 @@ impl<'a, T: FromU16> Reader<'a, T> {
}
}

impl<'a, T: FromU16> FromPointerReader<'a> for Reader<'a, T> {
impl<'a, T: TryFrom<u16, Error = NotInSchema>> FromPointerReader<'a> for Reader<'a, T> {
fn get_from_pointer(
reader: &PointerReader<'a>,
default: Option<&'a [crate::Word]>,
Expand All @@ -75,27 +75,29 @@ impl<'a, T: FromU16> FromPointerReader<'a> for Reader<'a, T> {
}
}

impl<'a, T: FromU16> IndexMove<u32, ::core::result::Result<T, NotInSchema>> for Reader<'a, T> {
impl<'a, T: TryFrom<u16, Error = NotInSchema>>
IndexMove<u32, ::core::result::Result<T, NotInSchema>> for Reader<'a, T>
{
fn index_move(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
self.get(index)
}
}

impl<'a, T: FromU16> Reader<'a, T> {
impl<'a, T: TryFrom<u16, Error = NotInSchema>> Reader<'a, T> {
/// Gets the `T` at position `index`. Panics if `index` is greater than or
/// equal to `len()`.
pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
assert!(index < self.len());
let result: u16 = PrimitiveElement::get(&self.reader, index);
FromU16::from_u16(result)
result.try_into()
}

/// Gets the `T` at position `index`. Returns `None` if `index`
/// is greater than or equal to `len()`.
pub fn try_get(&self, index: u32) -> Option<::core::result::Result<T, NotInSchema>> {
if index < self.len() {
let result: u16 = PrimitiveElement::get(&self.reader, index);
Some(FromU16::from_u16(result))
Some(result.try_into())
} else {
None
}
Expand All @@ -116,7 +118,7 @@ pub struct Builder<'a, T> {
builder: ListBuilder<'a>,
}

impl<'a, T: ToU16 + FromU16> Builder<'a, T> {
impl<'a, T: ToU16 + TryFrom<u16, Error = NotInSchema>> Builder<'a, T> {
pub fn len(&self) -> u32 {
self.builder.len()
}
Expand All @@ -138,7 +140,7 @@ impl<'a, T: ToU16 + FromU16> Builder<'a, T> {
}
}

impl<'a, T: FromU16> FromPointerBuilder<'a> for Builder<'a, T> {
impl<'a, T: TryFrom<u16, Error = NotInSchema>> FromPointerBuilder<'a> for Builder<'a, T> {
fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a, T> {
Builder {
builder: builder.init_list(TwoBytes, size),
Expand All @@ -156,21 +158,21 @@ impl<'a, T: FromU16> FromPointerBuilder<'a> for Builder<'a, T> {
}
}

impl<'a, T: ToU16 + FromU16> Builder<'a, T> {
impl<'a, T: ToU16 + TryFrom<u16, Error = NotInSchema>> Builder<'a, T> {
/// Gets the `T` at position `index`. Panics if `index` is greater than or
/// equal to `len()`.
pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
assert!(index < self.len());
let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
FromU16::from_u16(result)
result.try_into()
}

/// Gets the `T` at position `index`. Returns `None` if `index`
/// is greater than or equal to `len()`.
pub fn try_get(&self, index: u32) -> Option<::core::result::Result<T, NotInSchema>> {
if index < self.len() {
let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
Some(FromU16::from_u16(result))
Some(result.try_into())
} else {
None
}
Expand All @@ -191,7 +193,7 @@ impl<'a, T> crate::traits::SetPointerBuilder for Reader<'a, T> {
}
}

impl<'a, T: FromU16> ::core::iter::IntoIterator for Reader<'a, T> {
impl<'a, T: TryFrom<u16, Error = NotInSchema>> ::core::iter::IntoIterator for Reader<'a, T> {
type Item = ::core::result::Result<T, NotInSchema>;
type IntoIter = ListIter<Reader<'a, T>, Self::Item>;

Expand Down
4 changes: 0 additions & 4 deletions capnp/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ pub trait ToU16 {
fn to_u16(self) -> u16;
}

pub trait FromU16: Sized {
fn from_u16(value: u16) -> ::core::result::Result<Self, crate::NotInSchema>;
}

pub trait IndexMove<I, T> {
fn index_move(&self, index: I) -> T;
}
Expand Down
52 changes: 29 additions & 23 deletions capnpc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,12 @@ pub fn getter_text(
primitive_case(&typ, &member, offset, f.to_bits(), 0),
(type_::Enum(_), value::Enum(d)) => {
if d == 0 {
Line(format!("::capnp::traits::FromU16::from_u16(self.{}.get_data_field::<u16>({}))",
Line(format!("::core::convert::TryInto::try_into(self.{}.get_data_field::<u16>({}))",
member, offset))
} else {
Line(
format!(
"::capnp::traits::FromU16::from_u16(self.{}.get_data_field_mask::<u16>({}, {}))",
"::core::convert::TryInto::try_into(self.{}.get_data_field_mask::<u16>({}, {}))",
member, offset, d))
}
}
Expand Down Expand Up @@ -1953,28 +1953,34 @@ fn generate_node(
Line("}".to_string()),
]));

output.push(
Branch(vec!(
Line(format!("impl ::capnp::traits::FromU16 for {} {{", last_name)),
Indent(Box::new(Line("#[inline]".to_string()))),
Indent(
Box::new(Branch(vec![
Line(
"fn from_u16(value: u16) -> ::core::result::Result<Self, ::capnp::NotInSchema> {".to_string()
),
Indent(
Box::new(Branch(vec![
Line("match value {".to_string()),
Indent(Box::new(Branch(match_branches))),
Line("}".to_string())
]))),
Line("}".to_string())]))),
output.push(Branch(vec![
Line(format!(
"impl ::core::convert::TryFrom<u16> for {} {{",
last_name
)),
Indent(Box::new(Line(
"type Error = ::capnp::NotInSchema;".to_string(),
))),
Indent(Box::new(Branch(vec![
Line(
"fn try_from(value: u16) -> ::core::result::Result<Self, Self::Error> {"
.to_string(),
),
Indent(Box::new(Branch(vec![
Line("match value {".to_string()),
Indent(Box::new(Branch(match_branches))),
Line("}".to_string()),
]))),
Line("}".to_string()),
Line(format!("impl ::capnp::traits::ToU16 for {} {{", last_name)),
Indent(Box::new(Line("#[inline]".to_string()))),
Indent(
Box::new(Line("fn to_u16(self) -> u16 { self as u16 }".to_string()))),
Line("}".to_string()))));
]))),
Line("}".to_string()),
Line(format!("impl ::capnp::traits::ToU16 for {} {{", last_name)),
Indent(Box::new(Line("#[inline]".to_string()))),
Indent(Box::new(Line(
"fn to_u16(self) -> u16 { self as u16 }".to_string(),
))),
Line("}".to_string()),
]));

output.push(Branch(vec![
Line(format!(
Expand Down
10 changes: 5 additions & 5 deletions capnpc/src/schema_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ pub mod node {
}
#[inline]
pub fn get_preferred_list_encoding(self) -> ::core::result::Result<crate::schema_capnp::ElementSize,::capnp::NotInSchema> {
::capnp::traits::FromU16::from_u16(self.reader.get_data_field::<u16>(13))
::core::convert::TryInto::try_into(self.reader.get_data_field::<u16>(13))
}
#[inline]
pub fn get_is_group(self) -> bool {
Expand Down Expand Up @@ -1111,7 +1111,7 @@ pub mod node {
}
#[inline]
pub fn get_preferred_list_encoding(self) -> ::core::result::Result<crate::schema_capnp::ElementSize,::capnp::NotInSchema> {
::capnp::traits::FromU16::from_u16(self.builder.get_data_field::<u16>(13))
::core::convert::TryInto::try_into(self.builder.get_data_field::<u16>(13))
}
#[inline]
pub fn set_preferred_list_encoding(&mut self, value: crate::schema_capnp::ElementSize) {
Expand Down Expand Up @@ -5991,9 +5991,9 @@ pub enum ElementSize {
Pointer = 6,
InlineComposite = 7,
}
impl ::capnp::traits::FromU16 for ElementSize {
#[inline]
fn from_u16(value: u16) -> ::core::result::Result<Self, ::capnp::NotInSchema> {
impl ::core::convert::TryFrom<u16> for ElementSize {
type Error = ::capnp::NotInSchema;
fn try_from(value: u16) -> ::core::result::Result<Self, Self::Error> {
match value {
0 => ::core::result::Result::Ok(Self::Empty),
1 => ::core::result::Result::Ok(Self::Bit),
Expand Down

0 comments on commit 3df12bf

Please sign in to comment.