Skip to content

Bump MSRV to 1.57 #788

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

Merged
merged 5 commits into from
Jul 16, 2025
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ name = "bytes"
# - Update CHANGELOG.md.
# - Create "v1.x.y" git tag.
version = "1.10.1"
edition = "2018"
rust-version = "1.39"
edition = "2021"
rust-version = "1.57"
license = "MIT"
authors = [
"Carl Lerche <me@carllerche.com>",
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.39"
msrv = "1.57"
8 changes: 4 additions & 4 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::buf::{limit, Chain, Limit, UninitSlice};
use crate::buf::{writer, Writer};
use crate::{panic_advance, panic_does_not_fit, TryGetError};

use core::{mem, ptr, usize};
use core::{mem, ptr};

use alloc::{boxed::Box, vec::Vec};

Expand Down Expand Up @@ -1503,7 +1503,7 @@ unsafe impl BufMut for &mut [u8] {
}

// Lifetime dance taken from `impl Write for &mut [u8]`.
let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt);
let (_, b) = core::mem::take(self).split_at_mut(cnt);
*self = b;
}

Expand Down Expand Up @@ -1559,7 +1559,7 @@ unsafe impl BufMut for &mut [core::mem::MaybeUninit<u8>] {
}

// Lifetime dance taken from `impl Write for &mut [u8]`.
let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt);
let (_, b) = core::mem::take(self).split_at_mut(cnt);
*self = b;
}

Expand Down Expand Up @@ -1600,7 +1600,7 @@ unsafe impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
// A vector can never have more than isize::MAX bytes
core::isize::MAX as usize - self.len()
isize::MAX as usize - self.len()
}

#[inline]
Expand Down
19 changes: 1 addition & 18 deletions src/buf/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,7 @@ impl<T: Buf> Buf for Take<T> {
}

const LEN: usize = 16;
let mut slices: [IoSlice<'a>; LEN] = [
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
IoSlice::new(&[]),
];
let mut slices: [IoSlice<'a>; LEN] = [IoSlice::new(&[]); LEN];

let cnt = self
.inner
Expand Down
15 changes: 7 additions & 8 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::iter::FromIterator;
use core::mem::{self, ManuallyDrop};
use core::ops::{Deref, RangeBounds};
use core::ptr::NonNull;
use core::{cmp, fmt, hash, ptr, slice, usize};
use core::{cmp, fmt, hash, ptr, slice};

use alloc::{
alloc::{dealloc, Layout},
Expand All @@ -16,7 +15,7 @@ use crate::buf::IntoIter;
#[allow(unused)]
use crate::loom::sync::atomic::AtomicMut;
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use crate::{offset_from, Buf, BytesMut};
use crate::{Buf, BytesMut};

/// A cheaply cloneable and sliceable chunk of contiguous memory.
///
Expand Down Expand Up @@ -1235,7 +1234,7 @@ unsafe fn promotable_to_vec(

let buf = f(shared);

let cap = offset_from(ptr, buf) + len;
let cap = ptr.offset_from(buf) as usize + len;

// Copy back buffer
ptr::copy(ptr, buf, len);
Expand Down Expand Up @@ -1263,7 +1262,7 @@ unsafe fn promotable_to_mut(
debug_assert_eq!(kind, KIND_VEC);

let buf = f(shared);
let off = offset_from(ptr, buf);
let off = ptr.offset_from(buf) as usize;
let cap = off + len;
let v = Vec::from_raw_parts(buf, cap, cap);

Expand Down Expand Up @@ -1348,7 +1347,7 @@ unsafe fn promotable_is_unique(data: &AtomicPtr<()>) -> bool {
}

unsafe fn free_boxed_slice(buf: *mut u8, offset: *const u8, len: usize) {
let cap = offset_from(offset, buf) + len;
let cap = offset.offset_from(buf) as usize + len;
dealloc(buf, Layout::from_size_align(cap, 1).unwrap())
}

Expand Down Expand Up @@ -1444,7 +1443,7 @@ unsafe fn shared_to_mut_impl(shared: *mut Shared, ptr: *const u8, len: usize) ->
let cap = shared.cap;

// Rebuild Vec
let off = offset_from(ptr, buf);
let off = ptr.offset_from(buf) as usize;
let v = Vec::from_raw_parts(buf, len + off, cap);

let mut b = BytesMut::from_vec(v);
Expand Down Expand Up @@ -1510,7 +1509,7 @@ unsafe fn shallow_clone_vec(
// vector.
let shared = Box::new(Shared {
buf,
cap: offset_from(offset, buf) + len,
cap: offset.offset_from(buf) as usize + len,
// Initialize refcount to 2. One for this reference, and one
// for the new clone that will be returned from
// `shallow_clone`.
Expand Down
13 changes: 6 additions & 7 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::iter::FromIterator;
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::ptr::{self, NonNull};
use core::{cmp, fmt, hash, isize, slice, usize};
use core::{cmp, fmt, hash, slice};

use alloc::{
borrow::{Borrow, BorrowMut},
Expand All @@ -17,7 +16,7 @@ use crate::bytes::Vtable;
#[allow(unused)]
use crate::loom::sync::atomic::AtomicMut;
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use crate::{offset_from, Buf, BufMut, Bytes, TryGetError};
use crate::{Buf, BufMut, Bytes, TryGetError};

/// A unique reference to a contiguous slice of memory.
///
Expand Down Expand Up @@ -694,7 +693,7 @@ impl BytesMut {
let v_capacity = v.capacity();
let ptr = v.as_mut_ptr();

let offset = offset_from(self.ptr.as_ptr(), ptr);
let offset = self.ptr.as_ptr().offset_from(ptr) as usize;

// Compare the condition in the `kind == KIND_VEC` case above
// for more details.
Expand Down Expand Up @@ -1722,7 +1721,7 @@ impl From<BytesMut> for Vec<u8> {
let shared = bytes.data as *mut Shared;

if unsafe { (*shared).is_unique() } {
let vec = mem::replace(unsafe { &mut (*shared).vec }, Vec::new());
let vec = core::mem::take(unsafe { &mut (*shared).vec });

unsafe { release_shared(shared) };

Expand Down Expand Up @@ -1797,7 +1796,7 @@ unsafe fn shared_v_to_vec(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> V
let shared = &mut *shared;

// Drop shared
let mut vec = mem::replace(&mut shared.vec, Vec::new());
let mut vec = core::mem::take(&mut shared.vec);
release_shared(shared);

// Copy back buffer
Expand All @@ -1823,7 +1822,7 @@ unsafe fn shared_v_to_mut(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> B
let v = &mut shared.vec;
let v_capacity = v.capacity();
let v_ptr = v.as_mut_ptr();
let offset = offset_from(ptr as *mut u8, v_ptr);
let offset = ptr.offset_from(v_ptr) as usize;
let cap = v_capacity - offset;

let ptr = vptr(ptr as *mut u8);
Expand Down
17 changes: 0 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ fn abort() -> ! {
#[inline(always)]
#[cfg(feature = "std")]
fn saturating_sub_usize_u64(a: usize, b: u64) -> usize {
use core::convert::TryFrom;
match usize::try_from(b) {
Ok(b) => a.saturating_sub(b),
Err(_) => 0,
Expand All @@ -124,7 +123,6 @@ fn saturating_sub_usize_u64(a: usize, b: u64) -> usize {
#[inline(always)]
#[cfg(feature = "std")]
fn min_u64_usize(a: u64, b: usize) -> usize {
use core::convert::TryFrom;
match usize::try_from(a) {
Ok(a) => usize::min(a, b),
Err(_) => b,
Expand Down Expand Up @@ -182,18 +180,3 @@ fn panic_does_not_fit(size: usize, nbytes: usize) -> ! {
size, nbytes
);
}

/// Precondition: dst >= original
///
/// The following line is equivalent to:
///
/// ```rust,ignore
/// self.ptr.as_ptr().offset_from(ptr) as usize;
/// ```
///
/// But due to min rust is 1.39 and it is only stabilized
/// in 1.47, we cannot use it.
#[inline]
fn offset_from(dst: *const u8, original: *const u8) -> usize {
dst as usize - original as usize
}
1 change: 0 additions & 1 deletion tests/test_buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bytes::buf::UninitSlice;
use bytes::{BufMut, BytesMut};
use core::fmt::Write;
use core::mem::MaybeUninit;
use core::usize;

#[test]
fn test_vec_as_mut_buf() {
Expand Down
2 changes: 0 additions & 2 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use std::panic::{self, AssertUnwindSafe};
use std::usize;

const LONG: &[u8] = b"mary had a little lamb, little lamb, little lamb";
const SHORT: &[u8] = b"hello world";
Expand Down Expand Up @@ -82,7 +81,6 @@ fn fmt() {
#[test]
fn fmt_write() {
use std::fmt::Write;
use std::iter::FromIterator;
let s = String::from_iter((0..10).map(|_| "abcdefg"));

let mut a = BytesMut::with_capacity(64);
Expand Down
Loading