Skip to content

Commit 66aa1d7

Browse files
bors[bot]japaric
andauthored
Merge #7
7: impl Index r=japaric a=japaric Co-authored-by: Jorge Aparicio <jorge@japaric.io>
2 parents 691cbf5 + 6c9ec93 commit 66aa1d7

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

.travis.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,8 @@ language: rust
33
matrix:
44
include:
55
- env: TARGET=x86_64-unknown-linux-gnu
6-
rust: beta
76

87
- env: TARGET=thumbv7m-none-eabi
9-
rust: beta
10-
11-
- env: TARGET=x86_64-unknown-linux-gnu
12-
rust: nightly
13-
14-
- env: TARGET=thumbv7m-none-eabi
15-
rust: nightly
168

179
before_install: set -e
1810

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
## [v0.3.2] - 2019-11-26
9+
10+
### Added
11+
12+
- `Aligned<_, [T]>` now implements the `Index<RangeTo<usize>>` trait; slicing
13+
this value to end returns an `Aligned<_, [T]>` slice.
14+
815
## [v0.3.1] - 2018-11-07
916

1017
### Changed
@@ -48,7 +55,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4855

4956
- Initial release
5057

51-
[Unreleased]: https://github.com/japaric/aligned/compare/v0.3.0...HEAD
58+
[Unreleased]: https://github.com/japaric/aligned/compare/v0.3.2...HEAD
59+
[v0.3.2]: https://github.com/japaric/aligned/compare/v0.3.1...v0.3.2
60+
[v0.3.1]: https://github.com/japaric/aligned/compare/v0.3.0...v0.3.1
5261
[v0.3.0]: https://github.com/japaric/aligned/compare/v0.2.0...v0.3.0
5362
[v0.2.0]: https://github.com/japaric/aligned/compare/v0.1.2...v0.2.0
5463
[v0.1.2]: https://github.com/japaric/aligned/compare/v0.1.1...v0.1.2

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ keywords = ["alignment", "aligned", "array", "static"]
88
license = "MIT OR Apache-2.0"
99
name = "aligned"
1010
repository = "https://github.com/japaric/aligned"
11-
version = "0.3.1"
11+
version = "0.3.2"
1212

1313
[dependencies]
1414
as-slice = "0.1.0"

ci/install.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
set -euxo pipefail
22

33
main() {
4-
if [ $TARGET != x86_64-unknown-linux-gnu ]; then
5-
rustup target add $TARGET
6-
fi
4+
rustup target add $TARGET
75
}
86

97
main

ci/script.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
set -euxo pipefail
22

33
main() {
4-
cargo check --target $TARGET
5-
6-
if [ $TRAVIS_RUST_VERSION = nightly ]; then
4+
if [ $TARGET = x86_64-unknown-linux-gnu ]; then
5+
cargo test --target $TARGET
6+
else
77
cargo check --target $TARGET
88
fi
99
}

src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ where
9191
}
9292
}
9393

94+
impl<A, T> ops::Index<ops::RangeTo<usize>> for Aligned<A, [T]>
95+
where
96+
A: sealed::Alignment,
97+
{
98+
type Output = Aligned<A, [T]>;
99+
100+
fn index(&self, range: ops::RangeTo<usize>) -> &Aligned<A, [T]> {
101+
unsafe { &*(&self.value[range] as *const [T] as *const Aligned<A, [T]>) }
102+
}
103+
}
104+
94105
impl<A, T> AsSlice for Aligned<A, T>
95106
where
96107
A: sealed::Alignment,
@@ -139,6 +150,22 @@ fn sanity() {
139150
assert_eq!(z.len(), 3);
140151
assert_eq!(w.len(), 3);
141152

153+
// alignment should be preserved after slicing
154+
let x: &Aligned<_, [_]> = &x;
155+
let y: &Aligned<_, [_]> = &y;
156+
let z: &Aligned<_, [_]> = &z;
157+
let w: &Aligned<_, [_]> = &w;
158+
159+
let x: &Aligned<_, _> = &x[..2];
160+
let y: &Aligned<_, _> = &y[..2];
161+
let z: &Aligned<_, _> = &z[..2];
162+
let w: &Aligned<_, _> = &w[..2];
163+
164+
assert!(x.as_ptr() as usize % 2 == 0);
165+
assert!(y.as_ptr() as usize % 4 == 0);
166+
assert!(z.as_ptr() as usize % 8 == 0);
167+
assert!(w.as_ptr() as usize % 16 == 0);
168+
142169
// alignment should be preserved after boxing
143170
let x: Box<Aligned<A2, [u8]>> = Box::new(Aligned([0u8; 3]));
144171
let y: Box<Aligned<A4, [u8]>> = Box::new(Aligned([0u8; 3]));
@@ -150,7 +177,7 @@ fn sanity() {
150177
assert_eq!(mem::align_of_val(&*z), 8);
151178
assert_eq!(mem::align_of_val(&*w), 16);
152179

153-
// test deref-ing
180+
// test coercions
154181
let x: Aligned<A2, _> = Aligned([0u8; 3]);
155182
let y: &Aligned<A2, [u8]> = &x;
156183
let _: &[u8] = y;

0 commit comments

Comments
 (0)