gap-buffer: Update for Rust 2018.

pull/13/head
Jim Blandy 3 years ago
parent bdd175151c
commit be527001d0

@ -2,5 +2,6 @@
name = "gap-buffer" name = "gap-buffer"
version = "0.1.0" version = "0.1.0"
authors = ["You <you@example.com>"] authors = ["You <you@example.com>"]
edition = "2018"
[dependencies] [dependencies]

@ -1,7 +1,8 @@
#![warn(rust_2018_idioms)]
#![allow(elided_lifetimes_in_paths)]
#![allow(dead_code)] #![allow(dead_code)]
mod gap { mod gap {
use std;
use std::ops::Range; use std::ops::Range;
/// A GapBuffer<T> is a sequence of elements of type `T` that can insert and /// A GapBuffer<T> is a sequence of elements of type `T` that can insert and
@ -42,24 +43,24 @@ mod gap {
} }
/// Return a pointer to the `index`'th element of the underlying storage, /// Return a pointer to the `index`'th element of the underlying storage,
/// as if the gap were not there. /// regardless of the gap.
/// ///
/// Safety: `index` must be less than self.capacity(). /// Safety: `index` must be a valid index into `self.storage`.
unsafe fn space(&self, index: usize) -> *const T { unsafe fn space(&self, index: usize) -> *const T {
self.storage.as_ptr().offset(index as isize) self.storage.as_ptr().offset(index as isize)
} }
/// Return a mutable pointer to the `index`'th element of the underlying /// Return a mutable pointer to the `index`'th element of the underlying
/// storage, as if the gap were not there. /// storage, regardless of the gap.
/// ///
/// Safety: `index` must be less than self.capacity(). /// Safety: `index` must be a valid index into `self.storage`.
unsafe fn space_mut(&mut self, index: usize) -> *mut T { unsafe fn space_mut(&mut self, index: usize) -> *mut T {
self.storage.as_mut_ptr().offset(index as isize) self.storage.as_mut_ptr().offset(index as isize)
} }
/// Return the offset in the buffer of the `index`'th element, taking /// Return the offset in the buffer of the `index`'th element, taking
/// the gap into account. This does not check whether index is in range, /// the gap into account. This does not check whether index is in range,
/// but it never returns the index of space in the gap. /// but it never returns an index in the gap.
fn index_to_raw(&self, index: usize) -> usize { fn index_to_raw(&self, index: usize) -> usize {
if index < self.gap.start { if index < self.gap.start {
index index
@ -193,18 +194,18 @@ mod gap {
std::ptr::drop_in_place(self.space_mut(i)); std::ptr::drop_in_place(self.space_mut(i));
} }
for i in self.gap.end .. self.capacity() { for i in self.gap.end .. self.capacity() {
drop(std::ptr::read(self.space(i))); std::ptr::drop_in_place(self.space_mut(i));
} }
} }
} }
} }
pub struct Iter<'a, T: 'a> { pub struct Iter<'a, T> {
buffer: &'a GapBuffer<T>, buffer: &'a GapBuffer<T>,
pos: usize pos: usize
} }
impl<'a, T: 'a> Iterator for Iter<'a, T> { impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T; type Item = &'a T;
fn next(&mut self) -> Option<&'a T> { fn next(&mut self) -> Option<&'a T> {
if self.pos >= self.buffer.len() { if self.pos >= self.buffer.len() {
@ -246,7 +247,7 @@ mod gap {
mod gap_tests { mod gap_tests {
#[test] #[test]
fn test() { fn test() {
use gap::GapBuffer; use super::gap::GapBuffer;
let mut buf = GapBuffer::new(); let mut buf = GapBuffer::new();
buf.insert_iter("Lord of the Rings".chars()); buf.insert_iter("Lord of the Rings".chars());
@ -259,7 +260,7 @@ mod gap_tests {
#[test] #[test]
fn misc() { fn misc() {
use gap::GapBuffer; use super::gap::GapBuffer;
let mut gb = GapBuffer::new(); let mut gb = GapBuffer::new();
println!("{:?}", gb); println!("{:?}", gb);
@ -301,7 +302,7 @@ mod gap_tests {
#[test] #[test]
fn drop_elements() { fn drop_elements() {
use gap::GapBuffer; use super::gap::GapBuffer;
let mut gb = GapBuffer::new(); let mut gb = GapBuffer::new();
gb.insert("foo".to_string()); gb.insert("foo".to_string());

Loading…
Cancel
Save