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

Switch from nightly toolchain to stable 1.81 #22

Merged
merged 3 commits into from
Sep 25, 2024
Merged
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
4 changes: 2 additions & 2 deletions recapn/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'a> SegmentPtr<'a> {
}

pub const fn signed_offset_from_end(self, offset: SignedSegmentOffset) -> Self {
const ONE: SignedSegmentOffset = SignedSegmentOffset::new(1).unwrap();
const ONE: SignedSegmentOffset = SignedSegmentOffset::new_unwrap(1);

self.signed_offset(ONE).signed_offset(offset)
}
Expand Down Expand Up @@ -370,7 +370,7 @@ pub struct Growing<A> {

impl<A> Growing<A> {
/// The default length used for the first segment of 1024 words (8 kibibytes).
pub const DEFAULT_FIRST_SEGMENT_LEN: AllocLen = AllocLen::new(1024).unwrap();
pub const DEFAULT_FIRST_SEGMENT_LEN: AllocLen = AllocLen::new_unwrap(1024);

#[inline]
pub const fn new(first_segment: AllocLen, alloc: A) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions recapn/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ impl<'a> Reader<'a> {
/// If the slice is too large to be in a message, this function panics.
#[inline]
pub const fn from_slice(slice: &'a [u8]) -> Self {
Self(
ptr::Reader::new(slice)
.expect("slice is too large to be contained within a cap'n proto message"),
)
let Some(r) = ptr::Reader::new(slice) else {
panic!("slice is too large to be contained within a cap'n proto message")
};
Self(r)
}

#[inline]
Expand Down
19 changes: 18 additions & 1 deletion recapn/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,31 @@ pub fn read_packed_from_stream<R: io::BufRead>(

#[cfg(feature = "std")]
pub fn write_message<W: io::Write>(mut w: W, segments: &MessageSegments<'_>) -> Result<(), io::Error> {
use std::io::IoSlice;

let stream_table = StreamTable::from_segments(segments);
let message_segment_bytes = segments.clone().into_iter().map(|s| s.as_bytes());
let mut io_slice_box = iter::once(stream_table.as_bytes())
.chain(message_segment_bytes)
.map(std::io::IoSlice::new)
.collect::<Box<[_]>>();

w.write_all_vectored(&mut io_slice_box)
// TODO(someday): This is literally a copy of write_all_vectored.
// Use it when it becomes stable.
let mut bufs = &mut *io_slice_box;

IoSlice::advance_slices(&mut bufs, 0);
while !bufs.is_empty() {
match w.write_vectored(bufs) {
Ok(0) => {
return Err(io::Error::new(io::ErrorKind::WriteZero, "failed to write whole buffer"));
}
Ok(n) => IoSlice::advance_slices(&mut bufs, n),
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}

#[cfg(feature = "std")]
Expand Down
2 changes: 0 additions & 2 deletions recapn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! A fast, safe, feature complete Cap'n Proto implementation for modern Rust.

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(const_option)]
#![cfg_attr(feature = "std", feature(write_all_vectored))]

use crate::alloc::AllocLen;
use core::fmt::{self, Display};
Expand Down
31 changes: 29 additions & 2 deletions recapn/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl NonZeroU29 {
pub const MIN_VALUE: u32 = 1;
pub const MAX_VALUE: u32 = 2u32.pow(29) - 1;

pub const MIN: Self = Self(NonZeroU32::new(Self::MIN_VALUE).unwrap());
pub const MAX: Self = Self(NonZeroU32::new(Self::MAX_VALUE).unwrap());
pub const MIN: Self = Self(unsafe { NonZeroU32::new_unchecked(Self::MIN_VALUE) });
pub const MAX: Self = Self(unsafe { NonZeroU32::new_unchecked(Self::MAX_VALUE) });

pub const ONE: Self = Self::MIN;

Expand All @@ -67,6 +67,15 @@ impl NonZeroU29 {
}
}

#[inline]
#[track_caller]
pub const fn new_unwrap(n: u32) -> Self {
let Some(s) = Self::new(n) else {
panic!("integer value out of range")
};
s
}

#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(n: u32) -> Self {
Expand Down Expand Up @@ -116,6 +125,15 @@ impl u29 {
}
}

#[inline]
#[track_caller]
pub const fn new_unwrap(n: u32) -> Self {
let Some(s) = Self::new(n) else {
panic!("integer value out of range")
};
s
}

#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(n: u32) -> Self {
Expand Down Expand Up @@ -194,6 +212,15 @@ impl i30 {
}
}

#[inline]
#[track_caller]
pub const fn new_unwrap(n: i32) -> Self {
let Some(s) = Self::new(n) else {
panic!("integer value out of range")
};
s
}

#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(n: i32) -> Self {
Expand Down
14 changes: 7 additions & 7 deletions recapn/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl StructSize {
/// ```
#[inline]
pub const fn len(self) -> ObjectLen {
ObjectLen::new(self.total()).unwrap()
ObjectLen::new_unwrap(self.total())
}

/// Gets the max number of elements an struct list can contain of this struct.
Expand Down Expand Up @@ -334,7 +334,7 @@ impl StructSize {
ElementCount::MAX
} else {
// subtract 1 for the tag ptr
ElementCount::new((ElementCount::MAX_VALUE - 1) / (self.total())).unwrap()
ElementCount::new_unwrap((ElementCount::MAX_VALUE - 1) / (self.total()))
}
}

Expand Down Expand Up @@ -420,7 +420,7 @@ struct Parts {
impl Parts {
/// Gets the segment offset of some content. This is only valid for struct and list pointers.
pub const fn content_offset(&self) -> SignedSegmentOffset {
SignedSegmentOffset::new(self.lower.get() as i32 >> 2).unwrap()
SignedSegmentOffset::new_unwrap(self.lower.get() as i32 >> 2)
}

/// Replace the segment offset of some content
Expand Down Expand Up @@ -591,7 +591,7 @@ struct StructPtr {
}

impl StructPtr {
pub const EMPTY: Self = Self::new(SignedSegmentOffset::new(-1).unwrap(), StructSize::EMPTY);
pub const EMPTY: Self = Self::new(SignedSegmentOffset::new_unwrap(-1), StructSize::EMPTY);

#[inline]
pub const fn new(offset: SignedSegmentOffset, StructSize { data, ptrs }: StructSize) -> Self {
Expand Down Expand Up @@ -2459,7 +2459,7 @@ impl<'a> StructReader<'a, Empty> {
///
/// As such any other use of this function is unsafe.
pub const unsafe fn new_unchecked(ptr: NonNull<Word>, size: StructSize) -> Self {
let ptrs_offset = SegmentOffset::new(size.data as u32).unwrap();
let ptrs_offset = SegmentOffset::new_unwrap(size.data as u32);

Self {
data_start: ptr.cast(),
Expand Down Expand Up @@ -2753,7 +2753,7 @@ impl<'a> ListReader<'a, Empty> {
element_size: ElementSize,
) -> Self {
let ptr = NonNull::new_unchecked(slice.as_ptr().cast_mut());
Self::new_unchecked(ptr, u29::new(element_count).unwrap(), element_size)
Self::new_unchecked(ptr, u29::new_unwrap(element_count), element_size)
}
}

Expand Down Expand Up @@ -3025,7 +3025,7 @@ impl<'a> BlobReader<'a> {
}
}

const LANDING_PAD_LEN: AllocLen = AllocLen::new(2).unwrap();
const LANDING_PAD_LEN: AllocLen = AllocLen::new_unwrap(2);

pub(crate) enum OrphanObject<'a> {
Struct {
Expand Down
16 changes: 10 additions & 6 deletions recapn/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ impl<'a> Reader<'a> {
pub const fn from_slice(s: &'a [u8]) -> Self {
match s {
[.., 0] => match ptr::Reader::new(s) {
Some(r) => Some(Self(r)),
None => None,
Some(r) => return Self(r),
None => {},
},
_ => None,
_ => {},
}
.expect("attempted to make invalid text blob from slice")

panic!("attempted to make invalid text blob from slice")
}

pub const fn byte_count(&self) -> ByteCount {
ByteCount::new(self.0.len().get()).unwrap()
ByteCount::new_unwrap(self.0.len().get())
}

/// The length of the text (including the null terminator)
Expand Down Expand Up @@ -193,7 +194,10 @@ impl<'a> Builder<'a> {
/// Returns the bytes of the text field without the null terminator
#[inline]
pub const fn as_bytes(&self) -> &[u8] {
let (_, remainder) = self.as_bytes_with_nul().split_last().unwrap();
let Some((_, remainder)) = self.as_bytes_with_nul().split_last() else {
// Originally we would panic here, but really anything is valid
return &[]
};
remainder
}

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-06-24"
channel = "1.81"
components = ["rustfmt", "clippy"]