Skip to content

Commit

Permalink
Assignment 1: Phase 1 Ferris Wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
fubupc committed May 23, 2023
1 parent 3435048 commit baa3253
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 32 deletions.
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-fail/modules-1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// FIXME: Prevent this file from compiling! Diff budget: 1 line.

mod a {
pub fn f() { }
fn f() {}
}

// Do not modify this function.
Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-fail/move.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// FIXME: Prevent this file from compiling! Diff budget: 1 line.
#[derive(Clone, Copy)]
// #[derive(Clone, Copy)]
struct MyType(usize);

// Note: do not modify this function.
Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/borrow-1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// FIXME: Make me compile! Diff budget: 1 line.

#[derive(Copy, Clone)]
struct MyType(usize);

// Do not modify this function.
Expand Down
6 changes: 4 additions & 2 deletions 1-shell/ferris-wheel/compile-pass/const.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// FIXME: Make me compile! Diff budget: 3 lines.

// #![feature(const_fn)]

const VAR: i32 = add(34, 10);

fn add(a: i32, b: i32) -> i32 {
const fn add(a: i32, b: i32) -> i32 {
a + b
}

fn main() { }
fn main() {}
1 change: 1 addition & 0 deletions 1-shell/ferris-wheel/compile-pass/derive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FIXME: Make me compile! Diff budget: 1 line.
#[derive(Debug, Clone, Copy)]
enum Duration {
MilliSeconds(u64),
Seconds(u32),
Expand Down
2 changes: 2 additions & 0 deletions 1-shell/ferris-wheel/compile-pass/feature-1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// FIXME: Make me compile! Diff budget: 2 lines.
// Do not modify this definition.
#![feature(i128_type)]

enum Duration {
MicroSeconds(u128),
MilliSeconds(u64),
Expand Down
4 changes: 2 additions & 2 deletions 1-shell/ferris-wheel/compile-pass/io-read-write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ struct ReadWrapper<T: io::Read> {
inner: T
}

impl io::Read for ReadWrapper<T> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
impl<T: io::Read> io::Read for ReadWrapper<T> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/lifetimes-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
struct StrWrapper<'a>(&'a str);

impl<'a> StrWrapper<'a> {
fn inner(&self) -> &str {
fn inner(&self) -> &'a str {
self.0
}
}
Expand Down
6 changes: 3 additions & 3 deletions 1-shell/ferris-wheel/compile-pass/lifetimes-2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// FIXME: Make me compile! Diff budget: 3 lines.

// Do not modify the inner type &'a T.
struct RefWrapper<T>(&'a T);
struct RefWrapper<'a, T: 'a>(&'a T);

impl RefWrapper {
fn inner(&self) -> &T {
impl<'a, T> RefWrapper<'a, T> {
fn inner(&self) -> &'a T {
self.0
}
}
Expand Down
4 changes: 2 additions & 2 deletions 1-shell/ferris-wheel/compile-pass/lifetimes-3.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// FIXME: Make me compile! Diff budget: 2 lines.

// Do not modify the inner type &'a T.
struct RefWrapper<T>(&'a T);
struct RefWrapper<'a, T: 'a>(&'a T);

// Do not modify the inner type &'b RefWrapper<'a, T>.
struct RefWrapperWrapper<T>(&'b RefWrapper<'a, T>);
struct RefWrapperWrapper<'a: 'b, 'b, T: 'a>(&'b RefWrapper<'a, T>);

pub fn main() { }
6 changes: 3 additions & 3 deletions 1-shell/ferris-wheel/compile-pass/lifetimes-4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// FIXME: Make me compile! Diff budget: 3 lines.

// Do not modify the inner type &'a T.
struct RefWrapper<T>(&'a T);
struct RefWrapper<'a, T: 'a>(&'a T);

// Do not modify the inner type &'b RefWrapper<'a, T>.
struct RefWrapperWrapper<T>(&'b RefWrapper<'a, T>);
struct RefWrapperWrapper<'a: 'b, 'b, T: 'a>(&'b RefWrapper<'a, T>);

impl RefWrapperWrapper {
impl<'a, 'b, T> RefWrapperWrapper<'a, 'b, T> {
fn inner(&self) -> &'a T {
(self.0).0
}
Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/mutability-1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// FIXME: Make me compile! Diff budget: 1 line.
fn make_1(v: &u32) {
fn make_1(v: &mut u32) {
*v = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/mutability-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FIXME: Make me compile! Diff budget: 1 line.

pub fn main() {
let x = 10;
let x = &mut 10;
*x = 20;
}
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/mutability-3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FIXME: Make me compile! Diff budget: 1 line.

pub fn main() {
let x = 10;
let mut x = 10;
x = 20;
}
4 changes: 2 additions & 2 deletions 1-shell/ferris-wheel/compile-pass/mutability-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
struct MyStruct(usize);

impl MyStruct {
fn make_1(&self) {
fn make_1(&mut self) {
self.0 = 1;
}
}

pub fn main() {
let x = MyStruct(10);
let mut x = MyStruct(10);
x.make_1();
}
4 changes: 2 additions & 2 deletions 1-shell/ferris-wheel/compile-pass/pattern-match-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum MyEnum {

fn matcher(val: &MyEnum) -> &str {
match *val {
MyEnum::A(string) => string.as_str(),
MyEnum::B(string) => string.as_str()
MyEnum::A(ref string) => string.as_str(),
MyEnum::B(ref string) => string.as_str()
}
}

Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/privacy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// FIXME: Make me compile! Diff budget: 1 line.

mod a {
fn f() { }
pub fn f() { }
}

// Do not modify this function.
Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/compile-pass/semi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// FIXME: Make me compile! Diff budget: 1 character.

fn add(a: i32, b: i32) -> i32 {
a + b;
a + b
}

fn main() { }
2 changes: 2 additions & 0 deletions 1-shell/ferris-wheel/compile-pass/trait-namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod a {
impl MyTrait for MyType { }
}

use a::MyTrait;

// Do not modify this function.
fn main() {
let x = a::MyType;
Expand Down
12 changes: 11 additions & 1 deletion 1-shell/ferris-wheel/compile-pass/try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ fn do_b() -> Result<u32, ErrorB> {
}

fn do_both() -> Result<(u16, u32), Error> {
Ok((do_a(), do_b()))
let a = match do_a() {
Err(e) => return Err(Error::A(e)),
Ok(a) => a,
};

let b = match do_b() {
Err(e) => return Err(Error::B(e)),
Ok(b) => b,
};

Ok((a, b))
}

fn main() { }
3 changes: 3 additions & 0 deletions 1-shell/ferris-wheel/run-pass/borrow-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ struct MyType(usize);

pub fn main() {
let mut x = MyType(1);

{
let y = &x;
assert_eq!(*y, MyType(1));
}

// Do not modify this line.
let x = &mut x;
Expand Down
26 changes: 23 additions & 3 deletions 1-shell/ferris-wheel/run-pass/builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
// FIXME: Make me pass! Diff budget: 30 lines.

#[derive(Default)]
struct Builder {
string: Option<String>,
number: Option<usize>,
}

impl Builder {
fn string<S: Into<String>>(mut self, s: S) -> Builder {
self.string = Some(s.into());
self
}

fn number(mut self, n: usize) -> Builder {
self.number = Some(n);
self
}

fn to_string(&self) -> String {
match (&self.string, self.number) {
(&Some(ref s), Some(n)) => format!("{} {}", s, n),
(&Some(ref s), None) => format!("{}", s),
(&None, Some(n)) => format!("{}", n),
(&None, None) => String::new(),
}
}
}

// Do not modify this function.
fn main() {
let empty = Builder::default().to_string();
Expand All @@ -31,9 +53,7 @@ fn main() {

assert_eq!(b, "bye now! 200");

let c = Builder::default()
.string("heap!".to_owned())
.to_string();
let c = Builder::default().string("heap!".to_owned()).to_string();

assert_eq!(c, "heap!");
}
12 changes: 12 additions & 0 deletions 1-shell/ferris-wheel/run-pass/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
#[derive(Debug, PartialOrd, PartialEq, Clone, Copy)]
struct IntWrapper(isize);

impl Ord for IntWrapper {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}

impl Eq for IntWrapper {}

fn max<I: Ord>(a: I, b: I) -> I {
std::cmp::max(a, b)
}

pub fn main() {
assert_eq!(max(1usize, 3), 3);
assert_eq!(max(1u8, 3), 3);
Expand Down
19 changes: 19 additions & 0 deletions 1-shell/ferris-wheel/run-pass/trait-impl.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
// FIXME: Make me pass! Diff budget: 25 lines.

#[derive(Debug)]
enum Duration {
MilliSeconds(u64),
Seconds(u32),
Minutes(u16)
}

impl PartialEq for Duration {
fn eq(&self, other: &Self) -> bool {
self.to_ms() == other.to_ms()
}
}

impl Duration {
fn to_ms(&self) -> u64 {
match self {
&Duration::MilliSeconds(x) => x,
&Duration::Seconds(x) => x as u64*1000,
&Duration::Minutes(x) => x as u64*1000*60,
}
}
}

use Duration::{Seconds, Minutes, MilliSeconds};

fn main() {
assert_eq!(Seconds(120), Minutes(2));
assert_eq!(Seconds(420), Minutes(7));
Expand Down
4 changes: 2 additions & 2 deletions 1-shell/ferris-wheel/run-pass/ufcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl FooToo for Dummy { }
fn main() {
let dummy = Dummy;

let x = dummy.foo();
let y = dummy.foo();
let x = Foo::foo(&dummy);
let y = FooToo::foo(&dummy);

// Values for `x` and `y` must come from calling `foo()` methods.
assert_eq!(x, 1);
Expand Down
2 changes: 1 addition & 1 deletion 1-shell/ferris-wheel/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function compile_test() {
return 0
fi

stderr=$(rustc "${file}" $RUSTC_FLAGS -Z no-trans 2>&1)
stderr=$(rustc "${file}" $RUSTC_FLAGS -Z no-codegen 2>&1)
result=$?

if $should_compile && [ $result -ne 0 ]; then
Expand Down

0 comments on commit baa3253

Please sign in to comment.